If you ever use paint application, I am sure that you know what is the use of crop tool. In this episode 7, I will tell you how to crop the image using opencv. it's very important when you want to take data in specific areas. Now Let's try it !!
And the result
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
IplImage* frame, * img1;
CvPoint point;
int drag = 0;
int key = 0;
bool pause = FALSE;
void mouseHandler(int event, int x, int y, int flags, void* param)
{
/* user press left button */
if (event == CV_EVENT_LBUTTONDOWN && !drag)
{
point = cvPoint(x, y);
drag = 1;
}
/* user drag the mouse */
if (event == CV_EVENT_MOUSEMOVE && drag)
{
cvDestroyWindow("result");
img1 = cvCloneImage(frame);
cvRectangle(img1,point,cvPoint(x, y),CV_RGB(255, 0, 0),1, 8, 0);
cvCopy(img1,frame, NULL);
cvShowImage("origin", img1);
pause = TRUE;
}
/* user move the mouse */
if (event == CV_EVENT_MOUSEMOVE && !drag)
{
point = cvPoint(x, y);
printf("x = %d y = %d \n",point.x,point.y);
}
/* user release left button */
int a,b;
if (event == CV_EVENT_LBUTTONUP && drag)
{
img1 = cvCloneImage(frame);
if ((x == point.x) || (y == point.y)) goto jmp;
if ((point.x > x) && (point.y < y)) goto jmp;
if ((point.x < x) && (point.y > y)) goto jmp;
if ((point.x > x) && (point.y > y)) {
a = point.x;
b = point.y;
point.x = x;
point.y = y;
x = a;
y = b;
}
cvSetImageROI(img1,cvRect(point.x,point.y,x - point.x,y - point.y));
cvNot(img1, img1); // or do whatever with the ROI
cvShowImage("result", img1);
cvResetImageROI(img1);
//cvCopy(img1,frame, NULL); //other tricks
//cvShowImage("result1", img1);
jmp:
drag = 0;
pause = FALSE;
}
/* user click right button: reset all */
if (event == CV_EVENT_RBUTTONUP)
{
cvShowImage("result", frame);
drag = 0;
}
}
int main(int argc, char *argv[])
{
/* create a window for the video */
cvNamedWindow( "origin", CV_WINDOW_AUTOSIZE );
cvSetMouseCallback("origin", mouseHandler, NULL);
while( key != 'q' )
{
frame = cvLoadImage("uniq.jpg");
if (!pause) cvShowImage("origin", frame);
key = cvWaitKey( 30 );
}
cvDestroyWindow("origin");
cvReleaseImage(&frame);
cvReleaseImage(&img1);
return 0;
}
And the result
2 comments:
it always crashes everytime i try to draw rectangle on the image.. any idea?
tx for sharing,btw :)
I have tried again with my program but it dont crash when I drew rectangle with different direction.
can u show me "the message error report" ?
Post a Comment