如何在图像中替换像素颜色与其他颜色? [英] How To Replace Pixel color with other color in Image?

查看:143
本文介绍了如何在图像中替换像素颜色与其他颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个形象,我可以阅读所有它的使用Martix像素颜色...我如何更改为RGB任何像素。如果我想它(黑色,红色或白色)

我的代码来读取矩阵的形象是:

 字符串sourceimg = @ D:\ProductionTools\Taskes\Image Processing\Test\001.jpg 
// ...
位图imageoriginal =新位图(sourceimg);
INT高度= imageoriginal.Height;
INT宽度= imageoriginal.Width;
色彩[] [] =嘉洛斯新的色彩[宽度] [];
的for(int i = 0; I<宽度;我++){
嘉洛斯[i] =新的色彩[身高];
为(INT J = 0; J<高度; J ++){
嘉洛斯[I] [J] =新的色彩();
嘉洛斯[I] [J] = imageoriginal.GetPixel(I,J);
}
}


解决方案

由于在您的评论中指出,该问题是关于颜色的距离,这是不是一件小事。这也涉及您使用的色彩空间,所以在这里我会告诉你在RGB,HSV,HLS,和CIELAB例子。此外,为了计算距离,你需要一个公式。为简单起见,让我们粘欧氏距离,如



然后再平凡回答你的问题,你计算的三个目标从目前的颜色的距离 PI 你有(黑,红,白)。最小的距离,表示你替换的颜色。在平局的情况下,我把那个给了最小距离最早的颜色。



现在,使用的色彩也是在这个任务非常重要,因为它建立了欧几里德的意义距离。下面是一个示例图片:





在RGB转换:





转换在HLS(即,RGB颜色被转换为HLS和计算的距离):





HSV:





CIELAB:




I have an Image and I can Read all it's Pixels colors using Martix ... How can I change The RGB for any Pixel If I want to convert it to the nearest color from (Black , Red or white)

My Code to Read the Image in Matrix is :

string sourceimg = @"D:\ProductionTools\Taskes\Image Processing\Test\001.jpg";
//...
Bitmap imageoriginal = new Bitmap(sourceimg);
int height = imageoriginal.Height;
int width = imageoriginal.Width;
Color[][] colormatrix = new Color[width][];
for (int i = 0; i < width; i++) {
    colormatrix[i] = new Color[height];
    for (int j = 0; j < height; j++) {
        colormatrix[i][j] = new Color();
        colormatrix[i][j] = imageoriginal.GetPixel(i, j);
    }
}  

解决方案

As pointed out in your comments, the question is about color distance, which is not a trivial matter. It also relates to which colorspace you use, so here I will show you examples in RGB, HSV, HLS, and CIELAB. Also, to calculate distance you need a formula. For simplicity, let's sticky to the euclidean distance, as in

Then to trivially answer your question, you calculate the distance from the current color q to the three targets pi you have (black, red, and white). The smallest distance indicates the color you replace with. In case of tie, I keep the earliest color that gave the minimum distance.

Now, the colorspace is also very important in this task since it establishes the meaning for the euclidean distance. Here is a sample image:

Converted in RGB:

Converted in HLS (i.e., the RGB color is converted to HLS and the distance is calculated):

HSV:

CIELAB:

这篇关于如何在图像中替换像素颜色与其他颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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