Asked By
thomas ward
250 points
N/A
Posted on - 05/13/2011
Hi.
I am a student of CSE. I am doing some project using open CV. I need to load a image and save each of the regions predefined in the program, as a separate image. How do i do it in C++?
For example, suppose I have a form with 8 fields, I shall give the pixels from where to crop and the lengths along with it.
The program should crop the image according to the location.
Trouble with OpenCV. Load a image
The concept you are talking about is called Region Of Interest. In short- "ROI". The function for setting region of interest in opencv is
cvSetImageROI();
It takes 2 arguments, first one is the image ( our subject) second one is a rectangle defining the area to be set for ROI.
Trouble with OpenCV. Load a image
OpenCV is a very powerful tool. If you want to do small works you can use CImg.
Link
It is simple and lightweight.
Trouble with OpenCV. Load a image
@Synthesis,
Thanks. But when I type this:
cvSetImageROI(img, 645,1468,565,75);
it says too many arguments.
@Technical
Thanks to you too. But I am afraid I have to do this in OpenCV.
Trouble with OpenCV. Load a image
Thomas,
By rectangle I literally meant a rectangle. Like this one: cvRect(645,1468,565,75)
So you should use : cvSetImageROI(img, cvRect(645,1468,565,75));
Trouble with OpenCV. Load a image
Thanks again synthesis. It was my misinterpretation. But now I have another problem:
cvSetImageROI(img, cvRect(390,290,280,60));
cvSaveImage("part1.bmp",img);
This works fine but when I add another one;
cvSetImageROI(img, cvRect(390,290,280,60));
cvSaveImage("part1.bmp",img);
cvSetImageROI(img, cvRect(1050,924,679,64));
cvSaveImage("part2.bmp",img);
This crashes just after I run it, giving some horrible printings in the screen. Stating cvImg out of bound. But my whole image is 3072×1024.
Trouble with OpenCV. Load a image
After the first call of setImageROI.
cvSetImageROI(img, cvRect(390,290,280,60));
Your image becomes 280×60. So next when you say
cvSetImageROI(img, cvRect(1050,924,679,64));
It can’t find the 1050 pixel within 280×60
You have to reset the ROI after each call
cvSetImageROI(img, cvRect(390,290,280,60));
cvSaveImage("part1.bmp",img);
cvResetImageROI(img);
cvSetImageROI(img, cvRect(1050,924,679,64));
cvSaveImage("part2.bmp",img);
cvResetImageROI(img);
Trouble with OpenCV. Load a image
You are most welcome Thank you from my heart!
That was a great support.