C++ cv::Scalar 未显示预期颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15916751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cv::Scalar not displaying expected color
提问by Protocole
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)
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
绘制椭圆,我想将椭圆颜色设置为绿色 [ RGB 值 : (165, 206, 94) ]。所以我将参数设置
const Scalar& color
为
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.
我也尝试过 RGB 替代品。
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.
但是显示的颜色是白色[ RGB value (255, 255, 255) ] ,而不是所需的绿色。
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;
:
让我把完整的相关代码放在这里。根据OpenCV iOS-Video Processing,这是以下CvVideoCamera
配置- (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:
然后在[self.videoCamera start];
调用后,(Mat&)image
将被捕获并可以在 CvVideoCameraDelegate 方法中进行处理- (void)processImage:(Mat&)image;
,这里是绘制椭圆的代码:
- (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.
最终,椭圆仍然是白色的,而不是想要的绿色。
回答by PowHu
Set alpha to 255 can fix this problem. Scalar(94,206,165,255)
将 alpha 设置为 255 可以解决这个问题。标量(94,206,165,255)
回答by Barshan Das
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.
由于 mrgloom 在评论中正确指向,这可能是因为您的图像类型[您要绘制的 Mat 对象,即 ellipse() 函数中的 Mat &img]。cv::Scalar(94, 206, 165) 是 8UC3 类型图像所需的绿色。在 32FC3 图像中设置这些值将导致白色。
回答by theroom101
you can use
您可以使用
src.convertTo(src, CV_8UC3);
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 docsafter that your ellipse should be green, if it doesn't help post the whole code.
其中 CV_8UC3 表示您使用 8 位无符号字符和 3 色图像表示。您可以在此处找到OpenCV 文档的更多信息, 之后您的椭圆应该是绿色的,如果它对发布整个代码没有帮助。
回答by Shakir
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:
我遇到了类似的问题,我设法通过首先将图像转换为 BGR 来解决它。所以在你的情况下 processImage 函数看起来像:
-(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:
我认为是 CV_8UC4 转换后它变成:
channels 3
depth 0
type 16
element size 3
which is CV_8UC3.
这是 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 的情况下不起作用的原因之一是当目标图像是opencv 文档中提到的 4 通道时,opencv 绘图函数不支持 alpha 透明度。所以通过转换 CV_RGBA2BGR 我们去掉了 alpha 通道。但是,话虽如此,如果我这样做,我就无法使其工作:
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.
在这个图像中,红色和蓝色颜色是倒置的。所以虽然它似乎有效,但我不确定这是否是实际原因。