English 中文(简体)
带有打开cv 的灰度图像颜色
原标题:color a grayscale image with opencv

i m 用于某些带有直流传感器的工程 。 i dd 喜欢给与深度映射的用户像素颜色 。 现在我有从白到黑的像素, 但我想要从红到黑。 我尝试过甲型混合, 但我的结果只是, 我从粉色到黑都有像素, 因为我添加( 加Weight) 红色+白 =粉色 。

这是我的实际代码:

layers = device.getDepth().clone();
cvtColor(layers, layers, CV_GRAY2BGR);

Mat red = Mat(240,320, CV_8UC3, Scalar(255,0,0));
Mat red_body; // = Mat::zeros(240,320, CV_8UC3);
red.copyTo(red_body, device.getUserMask());

addWeighted(red_body, 0.8, layers, 0.5, 0.0, layers);

设备. getDepth () 返回 cv:: 带有深度地图和设备的 Mat 。 getUserMask () 返回 cv:: 使用用户像素( 只有白色像素) 的 Mat 返回 cv: (只有白色像素)

有什么建议吗?

EDIT: one more thing: thanks to sammy answer i ve done it. but actually i don t have values exactly from 0 to 255, but from (for example) 123-220.

i 将通过一个简单的环形图找到最小值和最大值(是否有更好的方法? ), 我如何将我的值从最小值到 0 - 255 进行映射?

最佳回答

首先, OpenCV s 默认颜色格式为 BGR 而不是 RGB。 所以, 您创建红色图像的代码应该是

Mat red = Mat(240,320, CV_8UC3, Scalar(0,0,255));

对于红到黑的颜色映射,您可以使用元素智慧乘法,而不是字母混合

Mat out = red_body.mul(layers, 1.0/255);

您可以使用 M 矩阵的最小值和最大值

double minVal, maxVal;
minMaxLoc(M, &minVal, &maxVal, 0, 0);

然后,您可以用一个因子来减去最小值和尺度

double factor = 255.0/(maxVal - minVal);    
 M = factor*(M -minValue)
问题回答

有点笨拙和缓慢, 但也许分裂的层, 复制红体( 把它复制成一个频道 Mat, 不是3) 到红色频道, 把它们合并成层?

其效果相同,但以重塑速度更快:

layers = device.getDepth().clone();
cvtColor(layers, layers, CV_GRAY2BGR);

Mat red = Mat(240,320, CV_8UC1, Scalar(255)); // One channel
Mat red_body; 
red.copyTo(red_body, device.getUserMask());

Mat flatLayer = layers.reshape(1,240*320); // presumed dimensions of layer
red_body.reshape(0,240*320).copyTo(flatLayer.col(0));

// layers now has the red from red_body




相关问题
using colors in calculated member

Im using this query in MDX for a calculate measure topcount(nonempty([StatusPlanes].[Status].Status.members,[Measures].[Planes]),1)(0).member_caption this will bring me this result Dimension1 ...

Using colors with MDX calculated measure

I m using this query in MDX for a calculated measure topcount(nonempty([StatusPlanes].[Status].Status.members,[Measures].[Planes]),1)(0).member_caption This will bring me this result Dimension1 ...

How can I convert a color image to grayscale in MATLAB?

I am trying to implement an algorithm in computer vision and I want to try it on a set of pictures. The pictures are all in color, but I don t want to deal with that. I want to convert them to ...

Hex colors: Numeric representation for "transparent"?

I am building a web CMS in which the user can choose colours for certain site elements. I would like to convert all colour values to hex to avoid any further formatting hassle ("rgb(x,y,z)" or named ...

How to change out-of-focus text selection color in Xcode?

Okay, I ll bite. I ve got really pleasant code/window colors set up in Xcode. Ordinarily, my selection color is very visible. When I am doing a project search and iterating through the results, ...

How to change Gmap markers color?

I ve a custom google map with different points: Markers[0] = new Array(new GMarker(new GLatLng(45.0, 9.0)), "Location1", "<strong>Address Line</strong><br/>Some information"); ...

热门标签