简体   繁体   English

在 MATLAB 中返回像素颜色

[英]return pixel colors in MATLAB

I am trying to analyze the pixel colors of an image with extension '.png' in MATLAB.我正在尝试在 MATLAB 中分析扩展名为“.png”的图像的像素颜色。 I want to return the amount of pixels that are colored within a certain range of a certain RGB value.我想返回在某个 RGB 值的某个范围内着色的像素数量。

I am trying to use the 'imread' function to analyze the image.我正在尝试使用'imread' 函数来分析图像。 It returns an array of values 0-255.它返回一个值 0-255 的数组。 How do I sort these values to return an amount of pixels close to a certain numerically defined color?如何对这些值进行排序以返回接近某个数字定义颜色的像素数量?

Given a specified tolerance tol , you can determine how many colours are within a certain range by splitting up the image into its three channels and checking if each colour in each channel is within the range.给定指定的容差tol ,您可以通过将图像分成三个通道并检查每个通道中的每种颜色是否在范围内来确定特定范围内有多少颜色。 Using nnz to check the total number of non-zero pixels is something to consider here.这里需要考虑使用nnz检查非零像素的总数。 You can create logical matrices that check if each channel is within a certain tolerance for each colour, then logical ANDing them all together and checking for the number of non-zero entries that results after this computation:您可以创建logical矩阵来检查每个通道是否在每种颜色的特定容差范围内,然后将它们全部进行逻辑与运算,并检查此计算后产生的非零条目的数量:

Something like this, given that your image is stored in im :像这样,假设您的图像存储在im

R = 100;
G = 128;
B = 123; %// Example
tol = 5; %// +/- 5 pixels
imd = double(im); %// For precision
num = nnz(abs(imd(:,:,1)-R) < tol & ...
          abs(imd(:,:,2)-G) < tol & ...
          abs(imd(:,:,3)-B) < tol);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM