Find white pixels using for loop in certain rows/columns

So i have created a program whereby I find pixels that are greater than 250 so i can identify missing areas of an RGB image. However due to the image having other white areas it detects some areas i dont want. So i want to create a for loop whereby i can search certain rows/columns for white / missing areas rather than it searching the whole image matrix as it is doing now.
threshold = 253;
white_areas = img1(:,:,1) > threshold & img1(:,:,2) > threshold & img1(:,:,3) > threshold;
white_areas = uint8(white_areas);
There is a part of the code which currently detects the white areas.

6 Comments

Instead of that long inequality, try :
threshold = 253;
white_areas = all(img1 > threshold, 3)
To help get the right answers for you, why do you need to convert logical array into uint8?
And what do you want to in the for loop after you find pixels with 1 (or 0) at a particular row (or col) number (or range)?
for r = 1:size(img1, 1)
%Find white pixels in this row?
%Next steps?
end
Basically what im trying to do is find the missing areas of an image ( 5 white sections of the image) and once ive found them, replace them with the corresponding sections from another image ( the perfect version of it) to try and recreate the original. I convert it to uint8 so it can recreate the image later in the code. I assume a for loops just a quicker way to go about it, what i want to do is be able to search the 5 sections rather than the whole image. For example the first missing section is within the top left so 150 x 300 say, and then so on for the other sections.
@.m: "pixels that are greater than 250" is not clear. Do you mean the RGB values and the threshold of 253 in your code?
"search certain rows/columns for white" - which "certain" rows? What have you tried so far and what is teh remaining problem? How can you distinguish the missing areas from the real white pixels?
@Jan Yes i mean the RGB values as you can see from the code above. Basically the missing areas are white or roughly white so around 250 - 255. I've set the threshold as 253 at the moment as otherwise it picks up other areas within the image that match the threshold. The 'certain' rows and columns are where the missing areas are. The remaining problem is i dont know how to search specific rows/columns for the threshold. Thats the problem im facing, ineed to be able to distinguish the real white areas from the missing areas and the only way is the fact i know the rows/columns that are missing.
From your description, I don't see a need for a for loop. You can use the exact same code you've been using so far (or better OCDER suggestion) on a portion of an image, either using simple indexing (if the portion is rectangular) or mask (if the shape of the portion is arbitrary). This will be faster and simpler than a loop.
Once you have that section of white pixels, I would think that keeping it as a logical array would be a lot simpler than converting it to uint8 or anything else. You can use the same logical mask on your replacement image rather than converting back and forth.
But rather than talking abstractely, give us an example input and replacement image, tell us exactly which portion needs to be used and we will show you the code to use for that.
@Guillaume So the first three areas are:
Y: 0 to 170 , X: 0 to 320
Y: 520 to 600, X: 130 to 370
Y: 80 to 190, X: 440 to 480
I should be able to do the rest once i understand how to actually code it and pick up the white areas in these sections

Sign in to comment.

 Accepted Answer

Since you have three areas, it's probably easier to use a mask. The way I'd do it:
%img1: image with white areas to fill
%img2: image to use to fill img1
iswhite = all(img1 > 253, 3); %find all white pixels in img1
%now create a mask for the 3 areas
mask = false(size(iswhite)); %base mask, false everywhere
mask(1:171, 1:321) = true; %1st area
mask(521:601, 131:371) = true; %2nd area
mask(81:191, 441:481) = true; %3rd area
fillmask = iswhite & mask; %only keep areas of iswhite covered by the mask
rgbfillmask = repmat(fillmask, [1 1 3]); %replicate mask across all 3 colour channels
img1(rgbfillmask) = img2(rgbfillmask); %ccopy masked area of img2 in img1

1 Comment

Right i see now how that works, thankyou :) Ive managed to use this as a template for the rest of the areas so now it seems to be working much better.

Sign in to comment.

More Answers (0)

Categories

Find more on Scripts in Help Center and File Exchange

Products

Release

R2018a

Asked:

on 15 Jan 2019

Edited:

on 15 Jan 2019

Community Treasure Hunt

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

Start Hunting!