Problem of generating 4 sub-images

1 view (last 30 days)
gg
gg on 14 Nov 2013
Edited: Image Analyst on 15 Nov 2013
I want to divide a color image into 4 sub-images which are also needed to be color images. However, I got the wrong images for my implementation. Please help me. Thanks!
% I= double(imread('2.jpg'));
% [m,n] = size(I);
%
% r = I(:,:,1); % Get the RED matrix
% g = I(:,:,2); % Get the GREEN matrix
% b = I(:,:,3); % Get the BLUE matrix
%
% [m,n] = size(r);
%
% imgr{1} = r(1:m/2, 1:n/2);
% imgr{2} = r(m/2+1:m, 1:n/2);
% imgr{3} = r(1:m/2, n/2+1:n);
% imgr{4}= r(m/2+1:m, n/2+1:n);
%
% imgg{1} = g(1:m/2, 1:n/2);
% imgg{2} = g(m/2+1:m, 1:n/2);
% imgg{3} = g(1:m/2, n/2+1:n);
% imgg{4}= g(m/2+1:m, n/2+1:n);
%
% imgb{1} = b(1:m/2, 1:n/2);
% imgb{2} = b(m/2+1:m, 1:n/2);
% imgb{3} = b(1:m/2, n/2+1:n);
% imgb{4}= b(m/2+1:m, n/2+1:n);
%
% final_image1(:,:,1)= imgr{1};
% final_image1(:,:,2)= imgg{1};
% final_image1(:,:,3)= imgb{1};
%
% final_image2(:,:,1)= imgr{2};
% final_image2(:,:,2)= imgg{2};
% final_image2(:,:,3)= imgb{2};
%
% final_image3(:,:,1)= imgr{3};
% final_image3(:,:,2)= imgg{3};
% final_image3(:,:,3)= imgb{3};
%
% final_image4(:,:,1)= imgr{4};
% final_image4(:,:,2)= imgg{4};
% final_image4(:,:,3)= imgb{4};
%
% filename = {final_image1,final_image2,final_image3,final_image4};
% q={'a1.bmp','a2.bmp','a3.bmp','a4.bmp'};
%
% for i=1:4
% imwrite(filename{i},q{i},'bmp');
% end

Answers (3)

Simon
Simon on 14 Nov 2013
Hi!
What exactly is going wrong?
  5 Comments
Simon
Simon on 15 Nov 2013
You do not neccesarily need the "uint8" conversion of "I" upon splitting the image. If "I" was not defined as double before it will automatically be of class uint8 after "imread".
Image Analyst
Image Analyst on 15 Nov 2013
Edited: Image Analyst on 15 Nov 2013
Wow, you really like to have a lot of code don't you? You could have done it in far less space simply by using imcrop() as I and David recommended. See demo code that I added in my answer to show you how simple it can be.

Sign in to comment.


Image Analyst
Image Analyst on 14 Nov 2013
Edited: Image Analyst on 15 Nov 2013
Why not simply use imcrop() ? It would take far far fewer lines of code.
And when you do n/2, if n is an odd number, you'll get a .5 fraction off the end of the number and that will not let you do 1:n/2, so you need to do
n1 = floor(n/2);
n2 = n1+1;
Then go from 1 to n1 and from n2 to end.
[rows, columns, numberOfColorChannels] = size(rgbImage);
c1 = floor(columns/2); % Middle column
c2 = c1+1;
r1 = floor(rows/2); % Middle row
r2 = r1+1;
upperLeft = imcrop(rgbImage, [1, 1, c1, r1]);
upperRight = imcrop(rgbImage, [c2, 1, c1, r1]);
lowerLeft = imcrop(rgbImage, [1, r2, c1, r1]);
lowerRight = imcrop(rgbImage, [c2, r2, c1, r1]);
See attached for a full blown demo using MATLAB standard demo image.

David Sanchez
David Sanchez on 15 Nov 2013
I = your_image;
[r c l] = size(I); % image dimensions
r2 = floor(r/2);
c2 = floor(c/2);
I1 = imcrop(I,[1 r2 1 c2];
I2 = imcrop(I,[r2+1 r 1 c2]);
I3 = imcrop(I,[1 r2 1 c2+1 c]);
I4 = imcrop(I,[r2+1 r c2+1 c]);
  1 Comment
gg
gg on 15 Nov 2013
I have tried it. But there are some mistakes to implement it.
if true
% code
% I= imread('2.jpg');
% [m,n] = size(I);
%
% % I = your_image;
% [r c l] = size(I); % image dimensions
% r2 = floor(r/2);
% c2 = floor(c/2);
% tempI{1} = imcrop(I,[1 r2 1 c2]);
% tempI{2} = imcrop(I,[r2+1 r 1 c2]);
% tempI{3} = imcrop(I,[1 r2 1 c2+1 c]);
% tempI{4}= imcrop(I,[r2+1 r c2+1 c]);
% q={'a1.bmp','a2.bmp','a3.bmp','a4.bmp'};
%
% for i=1:4
% imwrite(uint8(tempI{i}),q{i},'bmp');
% end
end

Sign in to comment.

Categories

Find more on Quadratic Programming and Cone Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!