Clipping a image into small images
3 views (last 30 days)
Show older comments
I am working on a project which is based on missing pixel restoration in a image using Back Projection for Lost Pixels(BPLP) .It is based on Principal Component Analysis(PCA).For that I need to clip the input image of size M x N into small images of size w x h .So I used the following code to clip the image (say,360x480):
for c=0:1:11
col=c*40+1;
for r=0:1:11
row=r*30+1;
clip_img=f(row:row+30-1,col:col+40-1);
end
end
The problems that I encountered in the above code are
- The clipped images got over written on the variable that I used.
- Individually I could not get the clipped images.
- I could not write the individual images to location using 'imwrite' function.
- As said the input image contains missing pixels,I found no way to separate known and unknown images.
Please help in coding the above mentioned problem.
Awaiting for your reply
[Edit: Added markup for code and numbered list. AU]
0 Comments
Accepted Answer
Image Analyst
on 7 Apr 2012
Well you need to call imwrite inside the loop - that way it doesn't matter if it gets overwritten because you've just saved it to disk. Okay, based on your comment the code might go something like this (untested):
[rows columns numberOfColorChannels] = size(originalImage);
% Get binary image of where there is text.
textPixels = originalImage < 20; % or > if text is white. Pick value that works.
% Fill in original image with boundary values wherever there is text.
filledImage = roifill(originalImage, textPixels);
folder = 'c:/users/public/documents'; % Or wherever.
% Scan image saving small sub-images.
smallRows = 30;
smallColumns = 40;
counter = 1;
for col = 1 : smallColumns : columns
for row = 1 : smallRows : rows
% Get small sub-image.
subImage = filledImage (row:row+smallRows - 1, col:col+smallColumns - 1);
% Construct file name.
baseFileName = sprintf('image_%d.png', counter);
fullFileName = fullfile(folder, baseFileName);
% Save the sub-image to disk.
imwrite(subImage , fullFileName);
% Increment the counter.
counter = counter + 1;
end
end
0 Comments
More Answers (1)
Image Analyst
on 5 Apr 2012
Ah yes, probably our second most commonly asked question: how do I divide an image into blocks (subimages)? Probably the most common answer is to use mat2cell(). I think I favor making it a 3D image instead of cells but you can do it either way.
Can you explain what you mean by "missing Pixels"? Do you mean pixels that are black (have value 0)?
You have no indexes for clip_img - that's why it gets overwritten on each loop iteration.
Are these color or grayscale images?
Why were you not able to use imwrite() to write out clip_img? Let us see your line of code for imwrite().
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!