I am developing an App that detects red little rectangles from an image. I have a code (gotten from this answer here) that apparently does that.
The code states that I need to use the imread()
method with the String file name as a parameter.
The steps: I open the camera. I take a picture, display it on screen and save it to the App's file directory. Then, using the code mentioned, I use the imread()
to reference the image and get the job done.
I don't know if I am doing something wrong or what, but mask.data
ALWAYS returns NULL
. I believe that the problem lies on the imread()
file path given wrong, thus Mat3b brg
is empty, and then the error is created.
Can someone enlighten me on that?
Below is the code I got so far:
//The snippet begins after the picture is taken
//First Part of Method - closing the camera and adding the result view
[photoCam stop];
resultView = [[UIImageView alloc] initWithFrame:imageView.bounds];
UIImage* result = image;
[resultView setImage:result];
resultView.center = imageView.center;
[self.view addSubview:resultView];
//Second Part of the Method - saving the image to a file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"image.png"];
[UIImagePNGRepresentation(result) writeToFile:filePath atomically:YES];
//Third Part of the Method - reading the red rectangles
Mat3b bgr = cv::imread("image.png"); // Here is where I believe the problem is. HELP!!!!
//Already tried the absolute path of the image, like
// "/var/mobile/Containers/Data/Application/BAB824A7-C7C1-4D1E-BB4A-43A003F30F1A/Documents/image.png"
// Didn't work either.
Mat3b hsv;
cvtColor(bgr, hsv, COLOR_BGR2HSV);
Mat1b mask1, mask2;
inRange(hsv, Scalar(0, 70, 50), Scalar(10, 255, 255), mask1);
inRange(hsv, Scalar(170, 70, 50), Scalar(180, 255, 255), mask2);
Mat1b mask = mask1 | mask2;
if (mask.data == NULL)
{
NSLog(@"mask.data == null");
}
else
{
namedWindow("Mask", CV_WINDOW_AUTOSIZE);
imshow("Mask", mask);
waitKey();
}
Thanks in advance.