在Matlab中只将黑色转换为白色 [英] To convert only black color to white in Matlab

查看:2619
本文介绍了在Matlab中只将黑色转换为白色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这线程将黑色同时转换成白色和白色到黑色。
我想只将黑色转换为白色。
我知道这线程关于这样做,我问,但我不明白什么地方出了问题。

I know this thread about converting black color to white and white to black simultaneously. I would like to convert only black to white. I know this thread about doing this what I am asking but I do not understand what goes wrong.

图片

代码

rgbImage = imread('ecg.png');
grayImage = rgb2gray(rgbImage); % for non-indexed images
level = graythresh(grayImage); % threshold for converting image to binary, 
binaryImage = im2bw(grayImage, level); 
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Make the black parts pure red.
redChannel(~binaryImage) = 255;
greenChannel(~binaryImage) = 0;
blueChannel(~binaryImage) = 0;
% Now recombine to form the output image.
rgbImageOut = cat(3, redChannel, greenChannel, blueChannel);
imshow(rgbImageOut);

这提供

红色似乎出现错误渠道。
黑色在RGB中只是(0,0,0),所以它的删除应该是每个(0,0,0)像素为白色(255,255,255)。
使用

Where seems to be something wrong in red color channel. The Black color is just (0,0,0) in RGB so its removal should mean to turn every (0,0,0) pixel to white (255,255,255). Doing this idea with

redChannel(~binaryImage) = 255;
greenChannel(~binaryImage) = 255;
blueChannel(~binaryImage) = 255;

给予

所以我必须误解在Matlab中的东西。蓝色不应该有任何黑色。

So I must have misunderstood something in Matlab. The blue color should not have any black. So this last image is strange.

如何只将黑色变成白色?
我想保留蓝色的ECG。

How can you turn only black color to white? I want to keep the blue color of the ECG.

推荐答案

有什么问题?

您想要检测图片的所有黑色部分,但不是真的

You want to detect all black parts of the image, but they are not really black

示例:

>

您的想法(或您的代码):

Your idea (or your code):

您首先对图像进行二值化,选择与不是像素对应的像素。简而言之,你做: if pixel> level;像素是某物

You first binarize the image, selecting the pixels that ARE something against the pixels that are not. In short, you do: if pixel>level; pixel is something

因此,你在这里有一个小的误解!

Therefore there is a small misconception you have here! when you write

% Make the black parts pure red.

应读为

% Make every pixel that is something (not background) pure red.

因此,当你做

redChannel(~binaryImage) = 255;
greenChannel(~binaryImage) = 255;
blueChannel(~binaryImage) = 255;

您正在做

% Make every pixel that is something (not background) white 
% (or what it is the same in this case, delete them).

因此,你应该得到的是一个完全白色的图像。图像不是完全白色的,因为有一些像素被标记为不是某物,部分背景的值 level ,如果您的图像0.6。

Therefore what you should get is a completely white image. The image is not completely white because there has been some pixels that were labelled as "not something, part of the background" by the value of level, in case of your image around 0.6.

一个可以想到的解决方案是手动将级别设置为0.05或类似,因此在灰色到二进制threholding中只选择黑色像素。 ,这将无法100%,如您所见,数字有一些非常黑的值。

A solution that one could think of is manually setting the level to 0.05 or similar, so only black pixels will be selected in the gray to binary threholding. But this will not work 100%, as you can see, the numbers have some very "no-black" values.

我如何尝试解决问题:

我会尝试找到你想要的颜色,从图像中提取那个颜色,然后删除离群值。

I would try to find the colour you want, extract just that colour from the image, and then delete outliers.

使用HSV我相信我在别的地方回答你如何使用HSV)。

Extract blue using HSV (I believe I answered you somewhere else how to use HSV).

rgbImage = imread('ecg.png');
hsvImage=rgb2hsv(rgbImage);
I=rgbImage;
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
th=0.1;
R((hsvImage(:,:,1)>(280/360))|(hsvImage(:,:,1)<(200/360)))=255;
G((hsvImage(:,:,1)>(280/360))|(hsvImage(:,:,1)<(200/360)))=255;
B((hsvImage(:,:,1)>(280/360))|(hsvImage(:,:,1)<(200/360)))=255;
I2= cat(3, R, G, B);

imshow(I2)

在这里,我们想获得最大的蓝色部分,将是我们的信号。因此,最好的方法似乎是首先将所有蓝色像素的图像二值化

Once here we would like to get the biggest blue part, and that would be our signal. Therefore the best approach seems to first binarize the image taking all blue pixels

% Binarize image, getting all the pixels that are "blue"
bw=im2bw(rgb2gray(I2),0.9999);

然后使用 bwlabel 独立像素岛屿。

And then using bwlabel, label all the independent pixel "islands".

% Label each "blob"
lbl=bwlabel(~bw);

最重复的标签是信号。所以我们找到它,并使用该标签从信号中分离背景。

The label most repeated will be the signal. So we find it and separate the background from the signal using that label.

% Find the blob with the highes amount of data. That  will be your signal.
r=histc(lbl(:),1:max(lbl(:)));
[~,idxmax]=max(r);
% Profit!
signal=rgbImage;
signal(repmat((lbl~=idxmax),[1 1 3]))=255;
background=rgbImage;
background(repmat((lbl==idxmax),[1 1 3]))=255;

这里有一个有信号,背景和差异的图)

Here there is a plot with the signal, background and difference (using the same equation as @rayryang used)

>

这篇关于在Matlab中只将黑色转换为白色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆