检查邻域中的像素值 [英] Check for pixel values in a neighborhood

查看:258
本文介绍了检查邻域中的像素值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个执行以下操作的MATLAB脚本:

I'm trying to write a MATLAB script that does the following:

给定:.jpg图像的像素坐标(x,y)
目标:在给定坐标的5个像素半径内检查是否存在特定值的像素。

Given: pixel coordinates(x,y) for a .jpg image Goal: Check, within a 5 pixel radius of given coordinates, if there is a pixel of a certain value.

例如,假设我给出了坐标(100,100),然后我想在我的图像中检查(100,100)附近的任何黑色像素(0,0,0)。也许,像素(103,100)和(104,100)可能具有值(0,0,0)。

For example, let's say I'm given the coordinates (100,100), then I want to check the neighborhood of (100,100) within my image for any pixels that are black (0,0,0). So perhaps, pixel (103, 100) and (104,100) might have the value (0,0,0).

当前代码

x_coord = uint32(coord(:,1));  
y_coord = uint32(coord(:,2));
count = 0;

for i = 1:length(x_coord)
    %(img(x,y) returns pixel value at that (x,y)
    %Note 0 = black. Indicating that, at that position, the image is just 
    % black 
    if img(x_coord(i),y_coord(i)) == 0 
        count = count + 1;
    end
end

目前只检查确切位置。不在当地社区。我怎么能延长这个?

It currently only checks at an exact location. Not in a local neighborhood. How to could I extend this?

编辑:另请注意,只要附近至少有一个像素值,我增加计数。我没有尝试枚举邻域中有多少像素具有该值,只是试图找到至少一个具有该值的像素的证据

Also note, as long as there as at least one pixel in the neighborhood with the value, I increment count. I'm not trying to enumerate how many pixels in the neighborhood have that value, just trying to find evidence of at least one pixel that has that value.

编辑:

即使我无法识别代码错误,我也无法得到我想要的确切结果。这是我正在使用的代码。

Even though I am unable to identify an error with the code, I am not able to get the exact results I want. Here is the code I am using.

val = 0; %pixel value to check
N = 50; % neighbourhood radius

%2D grid of coordinates surrounding center coordinate
[R, C] = ndgrid(1 : size(img, 1), 1 : size(img, 2));

for kk = 1 : size(coord, 1)
    r = coord(kk, 1); c = coord(kk, 2); % Get pixel locations

    % mask of valid locations within the neighbourhood (avoid boundary problems)
    mask = (R - r).^2 + (C - c).^2 <= N*N;         

    pix = img(mask); % Get the valid pixels
    valid = any(pix(:) ~= val);
    % Add either 0 or 1 depending if we have found any matching pixels
    if(valid == 1)
        img = insertMarker(img, [r c], 'x', 'color', 'red', 'size', 10);
        imwrite(img, images(i).name,'tiff');
    end
    count = count + valid; 
end


推荐答案

更简单的方法将使用索引来获取一个邻域,然后检查邻域中的任何像素是否具有您正在寻找的值,使用 任何 这个社区的扁平化版本。抓住正确邻域的技巧是首先生成一个跨越图像整个维度的二维坐标网格,然后简单地使用圆的方程,其中心是您正在查看的每个坐标,并确定那些位置满足以下等式:

An easier way to do this would be to use indexing to grab a neighbourhood, then to check to see if any of the pixels in the neighbourhood have the value that you're looking for, use any on a flattened version of this neighbourhood. The trick with grabbing the right neighbourhood is to first generate a 2D grid of coordinates that span the entire dimensions of your image, then simply use the equation of a circle with the centre of it being each coordinate you are looking at and determine those locations that satisfy the following equation:

(x - a)^2 + (y - b)^2 <= N^2

N 是观察窗口的半径,(a,b)是感兴趣的坐标,而(x,y)是图像中的坐标。使用 meshgrid 生成坐标。

N is the radius of the observation window, (a, b) is a coordinate of interest while (x, y) is a coordinate in the image. Use meshgrid to generate the coordinates.

您可以使用上面的等式创建逻辑掩码,为图像编制索引拉出蒙版内有效的位置,并检查与所需像素匹配的像素数。上述方法的另一个好处是您不会受到任何越界错误的影响。因为您正在预生成图像中所有有效坐标的列表,所以生成蒙版会将您限制在图像的边界内,因此您无需检查超出边界条件....即使您指定要搜索的坐标超出范围。

You would use the above equation to create a logical mask, index into your image to pull the locations that are valid within the mask and check how many pixels match the one you want. Another added benefit with the above approach is that you are not subject to any out of bounds errors. Because you are pre-generating the list of all valid coordinates in your image, generating the mask will confine you within the boundaries of the image so you never have to check for out of boundaries conditions.... even when you specify coordinates to search that are out of bounds.

具体来说,假设您的图像存储在 img 中,您可以:

Specifically, assuming your image is stored in img, you would do:

count = 0; % Remembers total count of pixels matching a value
val = 0; % Value to match
N = 50; % Radius of neighbourhood

% Generate 2D grid of coordinates
[x, y] = meshgrid(1 : size(img, 2), 1 : size(img, 1));

% For each coordinate to check...
for kk = 1 : size(coord, 1)
    a = coord(kk, 1); b = coord(kk, 2); % Get the pixel locations
    mask = (x - a).^2 + (y - b).^2 <= N*N; % Get a mask of valid locations
                                           % within the neighbourhood        
    pix = img(mask); % Get the valid pixels
    count = count + any(pix(:) == val); % Add either 0 or 1 depending if 
                                        % we have found any matching pixels
end

这篇关于检查邻域中的像素值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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