0

I am detecting all the regions in an image which contain square shape. I get the detected regions containing squares in terms of its four coordinates (e.g. A,B,C,D) as shown below:

image

After detecting the region where the square is present i need to create the histogram of the region. At the moment, first of all i am creating separate image for each region and then i send each image to getHistogram(Mat detectedSquare); to get histogram.

Problem: The computational time is very very high for my application so, i want to find some method in which i can skip creating this separate separate squares for each region.

What i want to do: To directly create the histogram for each region without creating an image for it.

Currenlty i am creating separate image for each region as following and i want to get rid of it:

Mat detectedSquare;
detectedSquare.create(rows, cols, CV_8UC3);
Rect regionOfInterest = Rect (min_x,min_y, rows, cols);
detectedSquare= original_Image(regionOfInterest);

getHistogram(Mat detectedSquare);
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
skm
  • 5,015
  • 8
  • 43
  • 104
  • How long does it take to create an image from one region? The most amount of work there would be a bunch of memcpy calls. – Chris O Feb 11 '14 at 15:02
  • Also, perhaps [this post](http://stackoverflow.com/questions/7041181/equivalent-to-cvsetimageroi-in-the-opencv-c-interface) can be of help, setting ROI on an image. – Chris O Feb 11 '14 at 15:06
  • @ChrisO: at the moment, i am doing the same thing which is present on the link you have given...but the computational time is very high for my application....my actual application is to send those squares to SVM for classification. – skm Feb 11 '14 at 15:34

1 Answers1

2

Suppose Rect roi is the sub region of image Mat mat you want to compute its histogram, you can directly call like this:

getHistgram(Mat(mat(Range(roi.y, roi.y+roi.height)
                 , Range(roi.x, roi.x+roi.width))));

Edit: As you commented in the following, you want to speed it up using multi-threading. Perhaps the simplest way is to use OpenMP.

vector<Rect> squares(N); // N square regions

#pragma omp parallel for
for (int i=0; i<N; i++)
{
    // ... compute histogram for squares[i] ...
}
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
  • its also similar to what i am using except writing the code in single line. But is there any way to reduce the computational time. – skm Feb 11 '14 at 16:49
  • @user2440724 I guess you cannot if you mean the running time. You have to loop all pixels to compute histogram no matter what. Right? – herohuyongtao Feb 11 '14 at 16:51
  • is it possible to do multithreading for the "for loop" because the result of each square is independent. – skm Feb 11 '14 at 17:00
  • @user2440724 The answer is definitely yes. :) – herohuyongtao Feb 11 '14 at 17:02
  • i dont know much about it...i read some articles but couldn't figure it out how should i do it...can you help me in that? – skm Feb 11 '14 at 17:19
  • @user2440724 Check out the updated answer for multi-threading. – herohuyongtao Feb 11 '14 at 18:11
  • i have heard about openMP...but do i just need to write `#pragma omp parallel` infornt of my for loop....that's all i need to do? – skm Feb 11 '14 at 18:15
  • @user2440724 You also need to open OpenMP option in the project setting. In VS, it's under `Properties > C++ > Open MP Support`. – herohuyongtao Feb 11 '14 at 18:18
  • i tried using `#pragma omp parallel for(int i=0; itotal; i+=4)` but then i got an error that `‘i’ was not declared in this scope`....i am using opencv in ubuntu with CMakeList.txt – skm Feb 11 '14 at 18:21
  • i am using gcc compiler `gcc version 4.6.3` but i think that openMP is not activated in it...i am trying to find some way to do so...but i am wondering that if its not activated then why didnt i get any error about that "pragma" stuff – skm Feb 11 '14 at 18:36
  • @user2440724 Not sure how to open OpenMP option in gcc. Try to search on Google first. Good luck. – herohuyongtao Feb 11 '14 at 18:37
  • thanks i am trying...but if it works then i just need to write just those 3-4 words and it will do the threading itself? – skm Feb 11 '14 at 18:39
  • @user2440724 Yes. OpenMP is such simple. :) – herohuyongtao Feb 11 '14 at 18:39