How can I access a matrix within a matrix?

1 次查看(过去 30 天)

I have a grayscale image that has been split up into 4 separate equally sized parts (for image analysis) from which I am trying to remove pixels that have an intensity less than the mean. To make the code short I have decided to make a for loop within a for loop that first accesses the image that I want (top right, top left ect) then an if loop to carry out the test regarding the mean. Currently I am having issues with getting each list as a whole, instead I get the first 4 values from bac_tl. Is there any way I can resolved this?

bac_list = [bac_tl, bac_tr, bac_bl, bac_br]; %this is a list of the values above
tl_mean = mean(bac_tl(:)); %the means of each separate image are found
tr_mean = mean(bac_tr(:));
bl_mean = mean(bac_bl(:));
br_mean = mean(bac_br(:));
mean_list = [tl_mean, tr_mean, bl_mean, br_mean]; %this is a list of the means
for i = 1:4
    list = bac_list(i)
    for z = 1:numel(list(i))
        if list(z) < mean_list(i)
           list(z) = 0; 
        end
    end
end

回答(1 个)

Image Analyst
Image Analyst 2016-7-20
Process each quadrant one at a time. Then do
meanGL = mean2(thisQuadrant);
thisQuadrant(thisQuadrant < meanGL) = 0; % Blacken pixels less than the mean gray level.
Repeat 3 more times for each quadrant. For example to get upper right quadrant:
thisQuadrant = grayImage(1:rows/2, columns/2+1:end); % Extract upper right.
Then do the code I gave. No need for "for" loops at all.
  2 个评论
Guillaume
Guillaume 2016-7-21
As per Image Analyst answer, you do not need to loop over the pixels. You can do the comparison for all of them at once.
Here is how I would code the whole thing:
%input: greyimage, the image to filter
rowdist = [floor(size(greyimage, 1)/2), ceil(size(greyimage, 1)/2)]; %calculate height of each subimage. If height is not divisible by 2, bottom image is 1 pixel taller
coldist = [floor(size(greyimage, 2)/2), ceil(size(greyimage, 2)/2)]; %%calculate width of each subimage. If width is not divisible by 2, right image is 1 pixel wider
splitimages = mat2cell(greyimage, rowdist, coldist); %split into 4 images
masks = cellfun(@(subimg) subimg < mean2(subimg), splitimages, 'UniformOutput', false); %create mask for each subimage. logical 1 (true) is where the pixel is smaller than the mean
greyimage(cell2mat(masks)) = 0; %rejoin masks into one matrix and set true pixels to 0.

请先登录,再进行评论。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by