OpenCV - drawing color outlines in grayscale images

I am working on a segmentation algorithm for medical images, and during the process, it should display an evolving outline on the original image.
I work with halftone JPEG. I use the drawContours function to display the outlines, but I am unable to draw the outline of the color. I would like to draw a red outline on the grayscale image, but it only looks black or white.
Here is the code section:

Mat_<uchar> matrix = imread(path,0); int row = matrix.rows; int col = matrix.cols; Mat_<uchar> maskInter(row,col); for (int i=0; i<row; i++) for (int j=0; j<col; j++) { if ((double)matrix(i,j) <0) {maskInter(i,j)=255;} else {maskInter(i,j)=0;} }; vector<vector<Point> > contours; vector<Vec4i> hierarchy; Mat mat_out = maskInter.clone(); findContours( mat_out, contours, hierarchy, CV_RETR_TREE , CHAIN_APPROX_SIMPLE); drawContours( img, contours, -1, RGB(255,0,0),1.5,8,hierarchy,2,Point()); namedWindow(title); moveWindow(title, 100, 100); imshow(title, img); waitKey(0); 

Can a color outline be displayed in grayscale on an image?
Thanks

+4
source share
2 answers

To draw and display colors, you will need a 3-channel image (RGB). Your Mat_<uchar> matrix is only one channel. You can convert a grayscale image to a color image as follows:

  // read image cv::Mat img_gray = imread(path,0); // create 8bit color image. IMPORTANT: initialize image otherwise it will result in 32F cv::Mat img_rgb(img_gray.size(), CV_8UC3); // convert grayscale to color image cv::cvtColor(img_gray, img_rgb, CV_GRAY2RGB); 
+9
source

Can a color outline be displayed in grayscale on an image?

If these are really shades of gray (1 byte per pixel) than not, you cannot. To draw color outlines, you must convert the image to RGB (3 bytes per pixel) using cvtColor and after that the outline color of the drawing.

0
source

Source: https://habr.com/ru/post/1446782/


All Articles