Roi extraction with cv::rect in opencv
I finally found a workarround for this problem:
--> use the function "cvtColor"
UIImage *in_image = [UIImage imageNamed:@"image013.jpg"]; // 150 x 150 pixel image
cv::Mat cv_in_image = [in_image CVMat];
NSLog(@"cv_in_image cols = %i", cv_in_image.cols);
NSLog(@"cv_in_image rows = %i", cv_in_image.rows);
cv::Rect rect1;
rect1.x = 28;
rect1.y = 27;
rect1.width = 100;
rect1.height = 100;
NSLog(@"rect1 x = %i", rect1.x);
NSLog(@"rect1 y = %i", rect1.y);
NSLog(@"rect1 width = %i", rect1.width);
NSLog(@"rect1 height = %i", rect1.height);
cv::Mat Image1 = cv_in_image(rect1);
// This is the workarround !!!!!!!!!!!
/ -----------------------------------
// Use cvtColor-function twice in a row...
cv::cvtColor(Image1, Image1, cv::COLOR_RGB2BGR); // after that function, the error is gone
cv::cvtColor(Image1, Image1, cv::COLOR_BGR2RGB); // I use the function a second time to get back the original color-set...
NSLog(@"Image1 cols = %i", Image1.cols);
NSLog(@"Image1 rows = %i", Image1.rows);
self.imageView0.image = [[UIImage alloc] UIImageFromCVMat:(Image1)];
The ROI can finally be created as can be seen in the image documentation below : (...Really not sure why this suddenly works that way with the UIImage-View in iOS !! ????)
iKK
Updated on June 04, 2022Comments
-
iKK almost 2 years
I am trying to extract the first quarter of a 100x100 pixel image. I was thinking of using the cv::rect function from a roi-definition (see code example below). Furthermore my original image is part of a vector-array (here in the code-example below, I only work with one image - later there will be several but this is probably irrelevant for the example here).
What I am observing is a strange behaviour: The newly created image "Test" seems to be ok until about half of its rows. Then it shows completely random pixels (see example image).
What is the cause ?
Image-description: left: orig 100x100 image / right: created "Test" image with errors...
std::vector<cv::Mat> Image(1); // create vector array of one 100x100 pixel image Image[0] = cv::imread(argv[1]); // or in iOS: Image[0] = [in_image CVMat]; cv::Rect roi1 = cv::Rect(0, 0, Image[0].cols/2, Image[0].rows/2); cv::Mat Test = Image[0](roi1); imshow("final_result", Test); // the Test image has error rows !!!!!!! Why ????????
Another example: The second example shows the original 150x150 pixel image to the left. The rectangle rect1 is 100x100pixel. Again - same problem: The cropped image shows good performance at the beginning.But from a certain row, the pixels get messed up (in this example they turned all black...). Below is the original iOS-code. What could be the problem ??? (I am using iOS-simulator - could this be the issue??
Code for example2:
UIImage *in_image = [UIImage imageNamed:@"image013.jpg"]; // 150 x 150 pixel image cv::Mat cv_in_image = [in_image CVMat]; NSLog(@"cv_in_image cols = %i", cv_in_image.cols); NSLog(@"cv_in_image rows = %i", cv_in_image.rows); cv::Rect rect1; rect1.x = 28; rect1.y = 27; rect1.width = 100; rect1.height = 100; NSLog(@"rect1 x = %i", rect1.x); NSLog(@"rect1 y = %i", rect1.y); NSLog(@"rect1 width = %i", rect1.width); NSLog(@"rect1 height = %i", rect1.height); cv::Mat Image1 = cv_in_image(rect1); NSLog(@"Image1 cols = %i", Image1.cols); NSLog(@"Image1 rows = %i", Image1.rows); self.imageView0.image = [[UIImage alloc] UIImageFromCVMat:(Image1)];
The NSLog sais:
cv_in_image cols = 150 cv_in_image rows = 150 rect1 x = 28 rect1 y = 27 rect1 width = 100 rect1 height = 100 Image1 cols = 100 Image1 rows = 100
Example2: Image to the left: 150x150 pixels / rectangle (Image1) to the right shows errors !! Here you can find the original image from example2: link
-
remi almost 10 yearsThis is quite strange. Does it happen if you dont store your image in a std::vector? Have you check the dimension of the rect, and the image test? Is this really the code you are using? Isn't there some color/grayscale conversion at some point?
-
Яois almost 10 yearsThis is quite strange.. I have tested the very same code and no clue about what is causing the error
-
Яois almost 10 years@remi the two of us writing "this is quite strange" at almost the same time is also quite strange.. :D
-
iKK almost 10 yearsThis is coincidence (not strange) :) From what you said, remi, I am not quite sure from your statement whether you were able to reproduce the error or not ??
-
iKK almost 10 yearsMy answers to your questions remi: Yes it happens also if I do not store the image in a std::vector. Yes, the dimension of rect are checked (they seem fine to me, cols=50, rows=50,.. same for the Test-image: cols=50, rows=50). And it is exactly the code I am using (except I use the iOS-version to read teh image (see comment at line two of my example-code). And no, there is no color-greyscale-conversion: The original image is a color-image that seems almost black-and-white. Same error occurs with greyscale-image (except that the noisy-error-pixels end up in grey as well)... Any other clues ???
-
Bull almost 10 yearsadd
imshow("Image[0]", Image[0]);
immediately after the other the otherimshow()
to see if the Mat really is corrupted. -
iKK almost 10 yearsI am not using imshow but the iOS conversion: self.imageView.image = [[UIImage alloc] UIImageFromCVMat:Test]; Could that create some problems ?
-
Bull almost 10 yearsOne would hope not, but ... (sorry I know nothing about iOS)
-
herohuyongtao almost 10 yearsCould you share the original image?
-
don_q almost 10 yearsI would try to display the original image with your code to see if that works.
-
iKK almost 10 yearsto user3275885: yep - that works perfectly !! I did test that already... Only the image created with the code "cv::Mat Image1 = cv_in_image(rect1);" seems to have an issue. The image-cropping does not work. Is there another way to do so ??
-