使用阈值分割图像的一部分

发布于 2024-12-15 08:46:39 字数 675 浏览 5 评论 0原文

我试图隔离并分割黄色车身以改变它的颜色。为了做到这一点,我需要将身体与图像分开识别。并继续用剩余的白色像素进行演讲。我使用 C#,这里的计划

Color d;
Color newColor = Color.YellowGreen;    
for(inti =0;i<carimage.Width;i++){
    for(intj =0;j<carimage.Height;j++){
        d = carimage.GetPixel(i, j);
            if(d.R == 255 && d.G==255 && d.B == 255)
                image.SetPixel(i, j, newColor );
    }
}

简单阈值处理将丢弃车身未正确分离的第二张图像。我尝试了 Aforge.net 填充孔图像过滤器,但阈值图像没有发生重大变化。我尝试使用滤色器,但由于主体的颜色变化,我没有返回正确的输出。任何人都可以为此提出建议和解决方案吗?

原始图像

原始图像

阈值图像

“阈值图像”

Im trying to isolate and segment the yellow car body to change the color of it. in order to do that i need to separately identify the body from the image. And continue oration with the remaining white pixels. And im using C#, here the plan

Color d;
Color newColor = Color.YellowGreen;    
for(inti =0;i<carimage.Width;i++){
    for(intj =0;j<carimage.Height;j++){
        d = carimage.GetPixel(i, j);
            if(d.R == 255 && d.G==255 && d.B == 255)
                image.SetPixel(i, j, newColor );
    }
}

simple thresholding will trow the second image where car body is not separated correctly. i tried Aforge.net Fill holes image filter but no significant change has been done to the threshold image. I tried to use color filter but it i did not return a correct output due to color vary of the body. can anyone suggest and solution for this?

Original Image

original image

Threshold Image

threshold image

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

软的没边 2024-12-22 08:46:39

您可能想要研究聚类,而不是阈值化。

作为快速和肮脏的测试,我增加了 HSB 空间中的图像亮度(使用 Mathematica):

亮度调整=图像[
Map[#^{1, 1, 0.2} &, ImageData[ColorConvert[img, "HSB"]], {2}],
色彩空间 -> “HSB”]

在此处输入图像描述

然后我使用了简单的 K-Nearest 聚类:

(簇 = ClusteringComponents[ColorConvert[brightnessAdjusted, "RGB"], 3,
方法-> "KMeans"]) // 着色

在此处输入图像描述

进行着色以查找图像中相似颜色的簇(还有更多) ,可能更合适的聚类算法,所以你应该尝试一下)。然后我可以调整其中一个簇的颜色:

Image[MapThread[If[#1 == 2, #2[[{1, 3, 2}]], #2] &, {clusters, ImageData[brightnessAdjusted]}, 2]]

在此处输入图像描述

如果您想使用阈值处理,您可能应该使用 CIE 色彩空间,因为该色彩空间中的欧几里得距离更接近人类的感知。

Instead of thresholding, you might want to look into clustering.

As a quick&dirty test, I've increased the image brightness in HSB space (using Mathematica):

brightnessAdjusted = Image[
Map[#^{1, 1, 0.2} &, ImageData[ColorConvert[img, "HSB"]], {2}],
ColorSpace -> "HSB"]

enter image description here

Then I've used simple K-Nearest clustering:

(clusters = ClusteringComponents[ColorConvert[brightnessAdjusted, "RGB"], 3,
Method -> "KMeans"]) // Colorize

enter image description here

to find clusters of similar colors in the image (there are many more, probably more suitable clustering algorithms, so you should experiment a little). Then I can just adjust the color in one of the clusters:

Image[MapThread[If[#1 == 2, #2[[{1, 3, 2}]], #2] &, {clusters, ImageData[brightnessAdjusted]}, 2]]

enter image description here

If you want to use thresholding, you should probably use a CIE color space, since euclidian distances in that color space are closer to human perception.

左岸枫 2024-12-22 08:46:39

几年前我有一个类似的项目。我不记得确切的细节,但想法是在图像上移动一个(不太小的)滑动窗口,并计算窗口内每个位置的平均强度(可能分别针对 R、G 和 B)。我用这些平均值填充了“阈值图像”,并从原始图像中减去它。某处存在缩放因子以及其他调整内容,但重点是,这种方法比使用恒定阈值要好得多。

I had a similar project few years ago. I can't remember the exact details, but the idea was to shift a (not too small) sliding window over the image, and calculate the average intensity (maybe for R, G and B separately) inside the window at each position. I filled a "threshold image" with these averages, and subtracted it from the original image. There was a scaling factor somewhere, and other tuning stuff, but the point is, such an approach was way better than using a constant threshold.

执笔绘流年 2024-12-22 08:46:39

如果您要使用一组阈值,则最好在色相饱和度值中选择黄色色调 色彩空间。请参阅相关的SO问题

If you are going to use a set of thresholds, you might be better of selecting yellow hues in the Hue Saturation Value colorspace. See the related SO question.

郁金香雨 2024-12-22 08:46:39
    I=imread('test.jpg'); 
    I=im2double(rgb2gray(I)); 
    BW=im2bw(I,0.64);imshow(BW)

给我:

image

通过查看图像的直方图,我得到了 0.64 阈值。我建议你使用MATLAB进行图像处理,因为它更容易。希望对您为图像着色有所帮助。

    I=imread('test.jpg'); 
    I=im2double(rgb2gray(I)); 
    BW=im2bw(I,0.64);imshow(BW)

Gives me :

image

I got the 0.64 threshold by looking at the image's histogram. I suggest you use MATLAB to do image processing as it is much easier. Hope that helps you in colouring the image.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文