image - 给图像一个黑色背景?

标签 image matlab image-processing opencv

有没有办法在 MATLAB 中去除白色背景并将其变成黑色?

假设我有这张图片:

enter image description here

当我应用答案中建议的代码时,我得到以下输出:这并不完美

enter image description here

最佳答案

正如 Andrey 所注意到的,问题在于并非所有背景像素都是“255 白色”。这可能是由于 JPEG 压缩算法造成的,也可能是因为图像中有水果的阴影。

为了解决这个问题,首先通过模糊图像(这是克服 JPEG 伪像所必需的)来获得水果区域的二值掩码,然后用一个非常高的值对图像进行阈值处理,但略低于 255。这里是Matlab 中的解决方案:

I = imread('http://i.stack.imgur.com/5p4jV.jpg'); % Load your image.
H = fspecial('gaussian'); % Create the filter kernel.
I = imfilter(I,H); % Blur the image.

Mask = im2bw(Ig, 0.9); % Now we are generating the binary mask.
I([Mask, Mask, Mask]) = 0; % Now we have the image.

这是输出(您也可以在 im2bw 中尝试不同的阈值):

enter image description here

关于image - 给图像一个黑色背景?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9178924/

相关文章:

WPF错误: Cannot find governing FrameworkElement for target element

android - 带有imageview的自定义进度条

swift - 调整 iPad2 图标图像大小 - swift

python - 在Python中垂直翻转图像

html - 宽度完整图像 - 保持纵横比并保持水平和垂直居中

css - Flexbox 图片不接受最大高度

matlab - 错误 : version `GLIBCXX_3.4.21' not found

matlab - 在Matlab中提取具有多个频率分量的信号

matlab - 在 Matlab 中操作稀疏矩阵

c# - 在没有安全问题的情况下捕获网站缩略图