OpenCV: draw a white(!) circle in CV_16UC1 mat - c++

I would like to draw a white circle in an matrix of type CV_16UC1.
That is basically what I do:
cv::Mat bla => Type CV_16UC1
cv::circle(bla, cv::Point(15, 15), 1, COLOR, 20);
I tried for Color:
cv::Scalar(0,0,0)
cv::Scalar(255,255,255)
UINT_MAX
but everything turns up black... any suggestions?

If I remember correctly that C1 means one channel image. Try to use a color with one demension: cv::Scalar(0xffff)
I use 0xffff because the 16U means 16 (unsigned) bits per channel.

Related

Access pixel value of mask using opencv

I got a problem where I need to access pixels of a opencv Mat image container.
I use opencv inRange function to create a mask. In that mask I need to check the value of different pixels, but I won't receive the values I expect to receive.
// convert image to hsv for better color-detection
cv::Mat img_hsv, maskR, maskY, mask1, mask2;
cv::cvtColor(image, img_hsv, cv::COLOR_BGR2HSV);
// Gen lower mask (0-5) and upper mask (175-180) of RED
cv::inRange(img_hsv, cv::Scalar(0, 50, 20), cv::Scalar(5, 255, 255), mask1);
cv::inRange(img_hsv, cv::Scalar(175, 50, 20), cv::Scalar(180, 255, 255), mask2);
// Merge the masks
cv::bitwise_or(mask1, mask2, maskR);
after that I try to read the pixel values where I got extremely high values and even nans, but most of them zeros, which is expected as the mask is only black and white
if (maskR.at<double>(position.x, position.y) == 255)
is there something I'm missing? I tried with double, uchar, int and float
when I print the mask, I can clearly see the 0 and 255 entries(no nans or strange numbers), but when I access them with the at() function, I wont get the same results.
The coordinates of the pixels should be in the range of the Mat as the dimension of the mask is 1080x1920 and non of the coordinates reach over that.
I got the dimension by using cv::size
I finally found the answer to my own question.
It works when I use uchar:
maskR.at<uchar>(position.x, position.y) == 255
I thought this wouldn't work because printing this with std::cout wouldn't give me an output, but the reason for that is that I forgot to cast uchar so it could be printed in the console

Splitting a color image into three channels using Open CV and C++?

I am new to Open CV, so please forgive me if my question sounds stupid. So, i was studying about this new concept of splitting a BGR channel to individual channels using the split function. I was reading this article(http://answers.opencv.org/question/37132/i-want-to-split-and-show-r-b-g-pictures-why-does-it-not-work/) and i could not understand the code. So, please can anyone explain me the following line of code as i am really wanted to understand the concept.
I did not understand the create blue channel part at all. Please can anyone explain me a bit?
src = imread("pic.png");
vector<mat> spl(3);
split(src,spl);
Mat empty_image = Mat::zeros(src.rows, src.cols, CV_8UC1);
Mat result_blue(src.rows, src.cols, CV_8UC3); // notice the 3 channels here!
// Create blue channel
Mat in1[] = { spl[0], empty_image, empty_image };
int from_to1[] = { 0,0, 1,1, 2,2 };
mixChannels( in1, 3, &result_blue, 1, from_to1, 3 );
imshow("blue image", result_blue);
What the code does is split the color image into 3 grayscale images, holding the intensities of the red, green and blue.
Then the code takes the blue channel and constructs a color image with a zero red and green, so that when you show it, it will show as bluish, and not just grayscale.
Th OpenCV Split Function:
cv:split(src, spl)
Takes a 3 channel 24bit (8 bit for each channel) of type CV_8UC3 and split it into type CV_8UC1 (GrayScale), that is a single channel image or type R, G and B separately.
If you take as an example a Green channel image from spl1 vector and do cv::imshow, you should notice that the green color from the original RGB image will appear as high intensity value in only Green channel image. Likewise for any other channel.
mixChannels( in1, 3, &result_blue, 1, from_to1, 3 );
mixChannel is basically, a function that copies some channel of source image to the new destination image. When using the function you need to specify the the number of channel you need to mix which in your case is 3.
cv::Mat result_blue(src.rows, src.cols, CV_8UC3);
Note that this variable of type cv::Mat is memory to hold the output image where the channels from src are mixed to the destination image.
1 specifies the number of matrix or image you want in the final output image, that is in cv::Mat result_blue.
int from_to1[] = { 0,0, 1,1, 2,2 };
This array specifies which channel from src needs to be mixed with which channel in the destination. That is 0 will be copied to 0 and so on.
The last param 3 in mixChannel specifies the number of channel pairs in the destination image.
You may also look at this function of merging channels.
The cv::merge() function on the other than takes multiple single channel image and merge it to produce a high level. Note that cv::merge also takes a int, the second param which specifies the number of channels you need to merge.
Lastly, I would suggest that you play with the function to understand them well.

Opencv create new image using cv::Mat

I'm new to opencv and i'm trying on some sample codes.
in one code, Mat gr(row1,col1,CV_8UC1,scalar(0));
int x = gr.at<uchar> (row,col);
And in another one,
Mat grHistrogram(301,260,CV_8UC1,Scalar(0,0,0));
line(grHistrogram,pt1,pt2,Scalar(255,255,255),1,8,0);
Now my question is if i used scalar(0) instead of scalar(0,0,0) in second code, The code doesn't work.
1.Why this happening since, Both create a Mat image structure.
2.what is the purpose of const cv:Scalar &_s.
I search the Documentaion from Opencv site (opencv.pdf,opencv2refman.pdf) and Oreilly's Opencv book. But couldn't find a explained answer.
I think i'm using the Mat(int _rows,int _cols,int _type,const cv:Scalar &_s) struct.
First, you need the following information to create the image:
Width: 301 pixels
Height: 260 pixels
Each pixel value (intensity) is 0 ~ 255: an 8-bit unsigned integer
Supports all RGB colors: 3 channels
Initial color: black = (B, G, R) = (0, 0, 0)
You can create the Image using cv::Mat:
Mat grHistogram(260, 301, CV_8UC3, Scalar(0, 0, 0));
The 8U means the 8-bit Usigned integer, C3 means 3 Channels for RGB color, and Scalar(0, 0, 0) is the initial value for each pixel. Similarly,
line(grHistrogram,pt1,pt2,Scalar(255,255,255),1,8,0);
is to draw a line on grHistogram from point pt1 to point pt2. The color of line is white (255, 255, 255) with 1-pixel thickness, 8-connected line, and 0-shift.
Sometimes you don't need a RGB-color image, but a simple grayscale image. That is, use one channel instead of three. The type can be changed to CV_8UC1 and you only need to specify the intensity for one channel, Scalar(0) for example.
Back to your problem,
Why this happening since, both create a Mat image structure?
Because you need to specify the type of the Mat. Is it a color image CV_8UC3 or a grayscale image CV_8UC1? They are different. Your program may not work as you think if you use Scalar(255) on a CV_8UC3 image.
What is the purpose of const cv:Scalar &_s ?
cv::Scalar is use to specify the intensity value for each pixel. For example, Scalar(255, 0, 0) is blue and Scalar(0, 0, 0) is black if type is CV_8UC3. Or Scalar(0) is black if it's a CV_8UC1 grayscale image. Avoid mixing them together.
You can create single channel image or multi channel image.
creating single channel image : Mat img(500, 1000, CV_8UC1, Scalar(70));
creating multi channel image : Mat img1(500, 1000, CV_8UC3, Scalar(10, 100, 150));
you can see more example and detail from following page.
https://progtpoint.blogspot.com/2017/01/tutorial-3-create-image.html

how to draw a rectangle in an image using open cv?

I want to draw a rectangle in an image using open cv c++?I read a function called CV::rectangle,can anyone explain how this function works?Or is there any other method which can be used to draw rectangle?
You are right, you can use cv::rectangle.
You should be able to draw something using this code
cv::rectangle( img, cv::Point2f( 10, 10 ), cv::Point2f(100, 100), cv::Scalar( 255, 0, 0 ) );
This will draw a red rectangle starting with top left at (10, 10) and bottom right at (100,100).
This also assumes that img has 3 channels of usigned int type, if the type is different, then you need to change the values in the Scalar.

cv::Scalar not displaying expected color

On an image frame, I use
void ellipse(Mat& img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
to draw an ellipse and I want to set the ellipse color to green [ RGB value : (165, 206, 94) ].
So I set the parameter const Scalar& color to
cv::Scalar(94.0, 206.0, 165.0, 0.0); // as BGR order, suppose the value is 0.0 - 255.0
cv::Scalar(94.0/255.0, 206.0/255.0, 165.0/255.0, 0.0); // suppose the value is 0.0 - 1.0
I also tried RGB alternative.
CV_RGB(165.0, 206.0, 94.0); // as RGB order, suppose the value is 0.0 - 255.0
CV_RGB(165.0/255.0, 206.0/255.0, 94.0/255.0); // suppose the value is 0.0 - 1.0
But the color being displayed is white [ RGB value (255, 255, 255) ] , not the desired green one.
What I missed at this point? Any suggestion please. Thank you.
EDIT:
Let me put whole related code here. According to OpenCV iOS - Video Processing, this is the CvVideoCamera config in - (void)viewDidLoad;:
self.videoCamera = [[CvVideoCamera alloc] initWithParentView:imgView];
[self.videoCamera setDelegate:self];
self.videoCamera.defaultAVCaptureDevicePosition = AVCaptureDevicePositionFront;
self.videoCamera.defaultAVCaptureSessionPreset = AVCaptureSessionPreset352x288;
self.videoCamera.defaultAVCaptureVideoOrientation = AVCaptureVideoOrientationPortrait;
self.videoCamera.defaultFPS = 30;
self.videoCamera.grayscaleMode = NO;
[self.videoCamera adjustLayoutToInterfaceOrientation:UIInterfaceOrientationPortrait];
Then after [self.videoCamera start]; called, the (Mat&)image would be captured and can be processed in the CvVideoCameraDelegate method - (void)processImage:(Mat&)image; and here are the code to draw an ellipse:
- (void)processImage:(Mat&)image {
NSLog(#"image.type(): %d", image.type()); // got 24
// image.convertTo(image, CV_8UC3); // try to convert image type, but with or without this line result the same
NSLog(#"image.type(): %d", image.type()); // also 24
cv::Scalar colorScalar = cv::Scalar( 94, 206, 165 );
cv::Point center( image.size().width*0.5, image.size().height*0.5 );
cv::Size size( 100, 100 );
cv::ellipse( image, center, size, 0, 0, 360, colorScalar, 4, 8, 0 );
}
Eventually, the ellipse is still in white, not the desired green one.
Set alpha to 255 can fix this problem.
Scalar(94,206,165,255)
As mrgloom points correctly in the comment, it might be because of type of your image [ the Mat object where you want to draw, i.e Mat &img in ellipse() function].
cv::Scalar(94, 206, 165) is the desired green color for 8UC3 type images. Setting these values in 32FC3 image will result in white color.
you can use
src.convertTo(src, CV_8UC3);
Where CV_8UC3 means that you use 8 bits unsigned char and 3 color image representation.
More information you can find here OpenCV docs
after that your ellipse should be green, if it doesn't help post the whole code.
I was having similar problem and I have managed to fix it by first converting image to BGR. So in your case processImage function would look like as:
-(void)processImage:(Mat&)image
{
cvtColor(image, image, CV_RGBA2BGR);
cv::Scalar colorScalar = cv::Scalar( 94, 206, 165 );
cv::Point center( image.size().width*0.5, image.size().height*0.5 );
cv::Size size( 100, 100 );
cv::ellipse( image, center, size, 0, 0, 360, colorScalar, 4, 8, 0 );
}
The only line which I have included in your code is:
cvtColor(image, image, CV_RGBA2BGR);
If you also log channel, depth and type information in the above function as follows:
NSLog(#"Before conversion");
NSLog(#"channels %d", image.channels());
NSLog(#"depth %d", image.depth());
NSLog(#"type %d", image.type());
NSLog(#"element size %lu", image.elemSize());
cvtColor(image, image, CV_RGBA2BGR);
NSLog(#"After conversion");
NSLog(#"channels %d", image.channels());
NSLog(#"depth %d", image.depth());
NSLog(#"type %d", image.type());
NSLog(#"element size %lu", image.elemSize());
you will see before conversion:
channels 4
depth 0
type 24
element size 4
which I think is CV_8UC4 and after conversion it becomes:
channels 3
depth 0
type 16
element size 3
which is CV_8UC3.
I guess one of the reason why it does not work without cvtColor is that the opencv drawing functions don't support alpha transparency when the target image is 4-channel as mentioned in opencv documentation. So by converting CV_RGBA2BGR we take out alpha channel. However having said that I do not managed to get it work if I do:
cvtColor(image, image, CV_RGBA2RGB);
In this Red and Blue colors are inverted in the image. So although it seems to work but I am not sure if it is the actual reason.