I need to understand the function of cropping intuitively

2 ビュー (過去 30 日間)
Vinit Sutar
Vinit Sutar 2019 年 8 月 16 日
回答済み: Udit Mahato 2020 年 7 月 25 日
%Read the image
img = imread('image.jpg');
%Get the size (rows and columns) of the image
[r,c] = size(img);
rr=r/3;
%Wrire code to split the image into three equal parts and store them in B, G, R channels
B=imcrop(img,[1,1,c-1,rr-1]);
G=imcrop(img,[1,1*rr,c-1,rr-1]);
R=imcrop(img,[1,2*rr,c-1,rr-1]);
%concatenate R,G,B channels and assign the RGB image to ColorImg variable
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
imshow(ColorImg)
I need to perform image cropping on the above mentioned image.
But I also need to understand each line of code mentioned above
  5 件のコメント
Guillaume
Guillaume 2019 年 8 月 16 日
Possibly, the original colour image has indeed been saved as a greyscale jpeg (a rarity) where for some reasons the original 3 colour channels had been concatenated (before encoding) vertically.
In which case, the given code would indeed reconstruct the original colour image. One would presume that the OP would be aware of this, as it's not something that's normally done.
However, the same could be achieved more simply with simple indexing or reshape and permute. Using imcrop for that is misleading.
img = imread('image.jpg');
assert(ndims(img) == 2, 'Did not expect colour image');
assert(mod(size(img, 1), 3) == 0, 'Image height is not a multiple of 3');
rgbimg = permute(reshape(img, [], 3, size(img, 2)), [1, 3, 2])
%or
%rgbimg = cat(3, img(1:end/3, :), img(end/3+(1:end/3), :), img(2*end/3+1:end, :))
would do the same as the original code more simply.
Image Analyst
Image Analyst 2019 年 8 月 16 日
Do it like this:
[rows, columns, numberOfColorChannels] = size(img)
what does numberOfColorChannels show in the command window?
Please attach an example image so we can correct the code.
imcrop() takes an array of [xLeft, yTop, widthInColumnsX, heightInRowsY].
Take care not to confuse x and y with rows and columns like many beginners do.

サインインしてコメントする。

回答 (2 件)

Udit Mahato
Udit Mahato 2020 年 7 月 25 日
%Read the image
img = imread('image.jpg');
subplot(1,2,1);
imshow(img)
%Get the size (rows and columns) of the image
[r,c] = size(img);
rowsForOneImage=r/3
%Wrire code to split the image into three equal parts and store them in B, G, R channels
fprintf('Cropping B from row %d to %d.\n', 1, rowsForOneImage);
B=imcrop(img,[1, 1, c- 1, rowsForOneImage - 1]);
fprintf('Cropping G from row %d to %d.\n', 1*rowsForOneImage + 1, 1*rowsForOneImage + rowsForOneImage);
G=imcrop(img,[1, 1*rowsForOneImage + 1, c - 1, rowsForOneImage - 1]);
fprintf('Cropping R from row %d to %d.\n', 2*rowsForOneImage + 1, 2*rowsForOneImage + rowsForOneImage);
R=imcrop(img,[1, 2*rowsForOneImage + 1, c - 1, rowsForOneImage - 1]);
%concatenate R,G,B channels and assign the RGB image to ColorImg variable
ColorImg(:,:,1) = R;
ColorImg(:,:,2) = G;
ColorImg(:,:,3) = B;
subplot(1, 2, 2);
imshow(ColorImg)

Athul Prakash
Athul Prakash 2019 年 8 月 23 日
Hey Vinit,
I see that you haven't provided the image file used by your code. Attach the relevant image file with the question next time to get better solutions.
It seems like Red, Green and Blue channels are stored in an unusual way in your 'image.jpg'. Typically, the RGB channels are stored along the third dimension. Therefore, a typical image array would have size like height x width x 3. The image you are working with seems to be stored in 2 dimensions only. To fit RGB values into 2 dimensions, the separate color channels have been concatenated vertically (row-wise, along dim1). Try running
img = imread('image.jpg');
imshow(img);
and you should see something like:
cat2.jpg
See what I mean?
Like I said, this is an unusual way to store an image, so your code is reassembling this image into the standard form of height x width x 3.
First, imcrop is used to vertically split the image into 3 and store in variables 'R', 'G' and 'B'. Then, these variables are added along the third dimension of a new variable 'ColorImg'.
imcrop is just a function that crops images in a rectangular way. In this case, values of [xmin, ymin, width, height] are given and the function returns the indicated portion cropped from the input image. See the documentation belowhttps://www.mathworks.com/help/images/ref/imcrop.html
Use of imcrop for this is unusual. As mentioned by others, reshape and permute are usually preferred for this sort of workflow.
clr_img = reshape(img, [rr 3 c]); % separates the diff color channels; but puts them along dim2.
clr_img = permute(img, [1 3 2]); % reorders the dims to put channels along dim3.
Hope it Helps!

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by