App Crashes when I try to get RGB values from cv::Mat

asked 2013-09-25 08:47:54 -0500

updated 2013-09-25 14:20:01 -0500

Moster gravatar image

I I am trying to get the R,G & B values of a pixel in a cv::Mat. Here is the method to get the value of B:

int blue = img.at<cv::vec3b>(row,col)[0];

The app Crashes without any error log with EXC_BAD_Excess on the this statement.

I also tried the following method but ran into same problem:(

uchar b = frame.data[frame.channels()frame.colsy + x + 0];

I have successfully added the opencv2.framework in my project. I have added the compiler flags to compile for objective-C++and the normal conversions from cv::Mat to UIImage and vice versa are working fine.

Can any one please help me figure out the reason for this crash??

Am I not allowed to use the "Mat::at(int i0, int i1)" method in Objective-C for iOS?

OR is there any alternative method to get the R,G & B values from a cv::Mat?

edit retag flag offensive close merge delete

Comments

1

Can you check which value img.type() returns?

Moster gravatar imageMoster ( 2013-09-25 14:23:06 -0500 )edit

@Moster: Thanks for the response. I just tried the img.type() and come to know a different thing. Actually here is what I am attempting: Saving 30 frames(cv::Mat) from the video using the delegate method of CvVideoCamera. Then trying to apply an algorithm on RGB values of each frame.

Now when I tried img.type() I came to know that it is not crashing for the first frame. It works fine for the first 24 frames, giving me img.type() = 24 in the console, and then for the 25th frame, it prints img.type() = 0. This is where its crashing.

Mr.Atif.Alvi gravatar imageMr.Atif.Alvi ( 2013-09-26 05:37:02 -0500 )edit
1

Ok. type 24 leads to the fact that you even have 4 channels and not just 3. So in this case you could use cv::vec4b, although it wont make a difference regarding the crash. What I find kind of strange is that to the end you get a 1 channel uchar image. So you would need to use image.at<uchar>(x,y). You could check of which type your image is before accessing the pixels.

Moster gravatar imageMoster ( 2013-09-26 06:34:36 -0500 )edit

huraahhhh.... I just figured it out :D

@Moster thanx alot.... your comment put me on the right track :)

Some how my camera was stoping after sending 24 frames, and the last 6 indexes in my array contained garbage data(I assume). So when the code tried to get the RGB value of 25th frame, it crashed.

The number of channels of last 6 Mats were also 1(it should be 4 as it s BGRA type) ,so checking this also hinted me towards right direction.

The Crashing problem is solved now and m moving ahead. Thanx alot again :)

Mr.Atif.Alvi gravatar imageMr.Atif.Alvi ( 2013-09-26 07:13:11 -0500 )edit