1

Really new to C++ so my apologies for such a question. Been trying out with these but they dont seem to work.

(I'm executing a template matching function in opencv - https://docs.opencv.org/3.4/de/da9/tutorial_template_matching.html)

Edit: Here is my code for my image, template and mask i used!

cv::Mat image = cv::Mat(height,width, CV16UC1, image); // image in short
cv::Mat temp;
image.convertTo(temp, CV32_F); // convert image to 32 bits

cv::image_template = cv::Mat(t_width, t_height, CV_32F, t_image); // template
cv::mask_template = cv::Mat(t_width, t_height, CV_32F, m_image); // mask

cv:: Mat img_display, result;
temp.copyTo(img_display); // image to display
int result_cols = temp.cols - image_template.cols + 1;
int result_rows = temp.rows - image_template.rows + 1;
result.create(result_rows, result_cols, CV32FC1);

// all the other code
matchTemplate(temp, image_template, result, 0, mask_template);
normalize( result, result, 0, 1, cv::NORM_MINMAX, -1, cv::Mat());

// localize the minimum and maximum values in the result matrix
double minVal;
double maxVal;
cv::Point minLoc;
cv::Point maxLoc;
cv::Point matchLoc;
minMaxLoc(result, &minVal, &maxVal, &minLoc, &maxLoc, cv::Mat());
// for match_method TM_SQDIFF we take lowest values
matchLoc = minLoc;


// display source image and result matrix , draw rectangle around highest possible matching area
cv::rectangle( img_display, matchLoc, cv::Point( matchLoc.x + image_template.cols, matchLoc.y + image_template.rows), cv::Scalar::all(255), 2, 8, 0);
cv::rectangle( result, matchLoc, cv::Point(matchLoc.x + image_template.cols, matchLoc.y + image_template.rows), cv::Scalar::all(255), 2, 8, 0);

This is the given code:

cv::rectangle( img_display, matchLoc, cv::Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), cv::Scalar::all(0), 2, 8, 0 );

Tried to change it with the following code snippets but doesn't seem to work.

cv::rectangle( img_display, matchLoc, cv::Point( matchLoc.x + templ.cols , matchLoc.y + templ.rows ), cv::Scalar(0,255,0) , 2, 8, 0 );

This doesn't work either

rectangle(ref, maxloc, Point(maxloc.x + tpl.cols, maxloc.y + tpl.rows), CV_RGB(0,255,0), 2);

Do let me know where I am wrong!

Gabriel
  • 438
  • 1
  • 5
  • 16
  • 1
    Welcome to Stack Overflow. Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Please provide a [mre]. – HansHirse Dec 18 '19 at 05:15
  • show the error message or describe the begavious – Micka Dec 18 '19 at 05:19
  • When I checked your code seems no problem and your color defining is also true. You can define color CV_RGB(r, g, b) or cv::Scalar((b), (g), (r), 0). I couldnt understand why it doesnt work. Do u get errors ? Or ur code works but u dont see just because of working on 1 channel(gray channel) and u try to get green color. – Yunus Temurlenk Dec 18 '19 at 05:21
  • The current output is always black (or white when i use `cv::Scalar::all(255)`, and the color doesnt change when i try to input the new code. – Gabriel Dec 18 '19 at 05:58
  • @Max You need to share your code before. Probably you are using only one channel. Thats why you cant get green color. – Yunus Temurlenk Dec 18 '19 at 06:25
  • Have added in my code snippets! Hope they help! – Gabriel Dec 18 '19 at 09:09
  • What do you mean by 'not working'? Share the exact error or the event you get. Besides check all the values before giving them to the rectangle command in console; including image's size. – MeiH Dec 21 '19 at 10:27

2 Answers2

1

First of all, you are trying to scale your pixels 0 to 255 but you cant do that because your image is a float type image(32FC1) float type image pixel values can be scaled 0.0 to 1.0.

You need to convert your image to 8UC to be able to colorize easily. But this way will also have several problems which mentioned here. OpenCV matchTemplate function always gives result in 32FC1 format so it is difficult to make some colorized things on this type image.

In your source image you can draw your rectangles with your desired color but not in float type. You can also check this link

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
0

Simply add

#define CV_RGB(r, g, b) 

on top of your code so that OpenCV knows it will use RGB color space instead of the default BGR.

And then draw your green rectangle this way.

rectangle(frame, Point(startX, startY), Point(endX, endY), CV_RGB(0, 255, 0), 2);
Dharman
  • 30,962
  • 25
  • 85
  • 135
Yannick
  • 59
  • 7