How to make a crop image with a white color result ?

조회 수: 1 (최근 30일)
DhaniAri
DhaniAri 2018년 10월 1일
답변: Image Analyst 2020년 12월 17일
%%Cropping column off 25%
file=imread('Airplane.tiff');
CR1=imcrop(file,[128 0 512 512]);
[x,y]=size(CR1);
CRP1=zeros(M,N);
for i=1:x
for j=1:y
CRP1(i,j+127)=CR1(i,j);
end
end
step = step + 1;figure(step); imshow(uint8(CRP1));title('The Watermarked Image with Croping column off 25%');
imwrite(uint8(CRP1),'24.tiff','tiff');
if I run the code the cropping result is a black color, how to change it into a white color? -Thanks for the help-
  댓글 수: 1
rafid jasim
rafid jasim 2020년 12월 17일
hi
please how can you get this result, i need that
thanks

댓글을 달려면 로그인하십시오.

채택된 답변

ANKUR KUMAR
ANKUR KUMAR 2018년 10월 1일
Do you want white space or not any space over cropped area. Refer this. Since you have not pasted the original image, I am taking your attached image as input.
A=imread('24.jpg');
subplot(1,2,1)
imshow(A)
subplot(1,2,2)
[m,n]=size(A);
imshow(A(round(m/2)-100:round(m/2)+100,round(m/2)-100:round(m/2)+100))
  댓글 수: 2
ANKUR KUMAR
ANKUR KUMAR 2018년 10월 1일
Or if you still wish white color over cropped portion, just replace the black portion with white color code.
file=imread('24.jpg');
CR1=imcrop(file,[128 0 512 512]);
[x,y]=size(CR1);
CRP1=zeros(x,y);
for i=1:x
for j=1:y
CRP1(i,j+200)=CR1(i,j);
end
end
AA=uint8(CRP1);
AA(AA==0)=255;
imshow(AA);
rafid jasim
rafid jasim 2020년 12월 17일
thanks

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

DhaniAri
DhaniAri 2018년 10월 1일
Thank u for the help. I look for the white color over cropped partition
  댓글 수: 2
Image Analyst
Image Analyst 2018년 10월 1일
You've accepted the answer so I guess it solved it for you, though you didn't explicitly say so.
DhaniAri
DhaniAri 2018년 10월 1일
I'm sorry, i forget to say it on my comment

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2020년 12월 17일
This will do it:
% Demo by Image Analyst, December, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image.
folder = [];
baseFileName = '24.jpg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(grayImage)
% Display the test image full size.
subplot(2, 2, 1);
imshow(grayImage, []);
axis('on', 'image');
caption = sprintf('Reference Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Mask to find the black part on the left.
threshold = 5; % Find pixels darker than this.
mask = (grayImage <= threshold);
% Display the image.
subplot(2, 2, 2);
% imhist(grayImage);
imshow(mask, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Find columns that are all zeros
columnsToTurnWhite = all(mask, 1);
% Find rows that are all zeros
rowsToTurnWhite = all(mask, 2);
mask = false(rows, columns); % Erase everything.
% Now turn those rows and columns true (white).
mask(:, columnsToTurnWhite) = true;
mask(rowsToTurnWhite, :) = true;
% Display the image.
subplot(2, 2, 3);
imshow(mask, []);
axis('on', 'image');
title('Mask Image', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Make the rgb image white there
grayImage(mask) = 255;
subplot(2, 2, 4);
imshow(grayImage, []);
axis('on', 'image');
title('Final Image', 'FontSize', fontSize, 'Interpreter', 'None');
fprintf('Done running %s.m ...\n', mfilename);
but notice that there is still a black band there. This is because you used JPG format for your image - a very bad idea. Because JPEG is a lossy image compression format, there are actually some pixels that are not pure zero in some of the dark columns. A good illustration for why you never want to use JPG format. Use PNG instead. To fix, raise the threshold to 25 on line 54.
threshold = 25; % Find pixels darker than this.
That code is attached. Note that the code works for both vertical and horizontal black bands that you want to turn white.

카테고리

Help CenterFile Exchange에서 Convert Image Type에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by