2

I'm working with some object detection using OpenCV 2.4.4 with C++. Unfortunately those objects that I must detect doesn't have unique shape, but they do have some really specific color range in the HSV color space (right now i'm detecting some red objects).

Also, the object should be present only in some part of the image, so I'm not scanning all the image, moreover, under this ROI I have sub-ROIs (little rectangles, and I need to know in which of these rectangles the object is in a frame).

So, first I tried to detect it something was found in the large ROI and if it was I set it to some color alien to my ambient. I did it with the following code:

cvtColor(frame(Range(0,espv),Range::all()),framhsv,CV_BGR2HSV); 
MatIterator_<Vec3b> it, ith, end, endh;
ith  = framhsv.begin<Vec3b>();
endh = framhsv.end<Vec3b>();
it   = (frame(Range(0,espv),Range::all())).begin<Vec3b>(); 
for (;ith != endh; ++it,++ith){
    if( (((*ith)[0] <=7 && (*ith)[0]>=0)||((*ith)[0]>170)) && (*ith)[1]>=160){
        (*it)[0] = 63;
        (*it)[1] = 0;
        (*it)[2] = 255;}}

Everything worked right and the object was "detected", so I passed to the next steep, which is detecting if the object was in each sub-ROIs. To do so I created the following function the returns the number o "detected" pixels in each sub-ROI, so I can consider which is the correct sub-ROI.

Here is the function:

Vector<int> CountRed(cv::Mat &frame, int hspa, int vspa, int nlines ,int nblocks){
    Vector<int> count(nblocks*nlines,0);
    for(int i = 1; i<= nlines; i++){
        for(int j = 1; j<= nblocks; j++){

            Mat framhsv;
            cvtColor(frame(Range((i-1)*vspa,i*vspa),Range((j-1)*hspa,j*hspa)),framhsv,CV_BGR2HSV); //Converte somente a área de interesse para HSV

            MatIterator_<Vec3b> it, ith, endh;
            ith  = framhsv.begin<Vec3b>();
            endh = framhsv.end<Vec3b>();
            it   = (frame(Range((i-1)*vspa,i*vspa),Range((j-1)*hspa,j*hspa))).begin<Vec3b>();

            for (;ith != endh; ++it,++ith){
                if( (((*ith)[0] <=7 && (*ith)[0]>=0)||((*ith)[0]>170)) && (*ith)[1]>=160){
                    (*it)[0] = 63;
                    (*it)[1] = 0;
                    (*it)[2] = 255;
                    count[(j)+nblocks*(i-1)-1]++;}}}}
return count;
}

The code compiles without warnings or errors and the video shows up, but if I insert the object that I must detect in any part of the ROI the code stop working, this also happens if I remove the:

count[(j)+nblocks*(i-1)-1]++;

line. I get the following error: Access violation writing location 0xFFFFF970.

I really think the problem must be in accessing the iterator when not using Range::All()

To try to clarify what I'm doing here is an image of a frame: https://i.stack.imgur.com/nPwZY.jpg

the purple region is the frame, the red region is the ROI, and each of the black ones are the sub-ROIs.

I've also tried using the frame(Rect(0,0,frame.cols,2*vspa) to define the ROI and it also worked, but I got the same error when trying to work with the sub-ROIs using cv::Rect. So, I really think this must be a MatIterator_ error when not accessing the full row structure.

So, what should I do to work with those sub-ROIs ?

Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
gustavo-vm
  • 131
  • 2
  • 10

0 Answers0