How to remove subplot grey space between images
Mostra commenti meno recenti
I am trying to remove the space between and cannot figure it out. I have tried many ways and nothing is working. Is there a way to remove the space between or maybe a different way to approach this problem. The goal is to have the images touching in a grid like way
This is what prints:

theImage = imread('BarackObama.jpg');
%theImage = imread(image);
grayScaleImage = rgb2gray(theImage);
theGrayImage = grayScaleImage > 100;
[height, width, dim] = size(theImage);
numRecsAcross = 4;
numRecsDown = 4;
xmin = 1;
ymin = 1;
width = (width/numRecsAcross);
height = (height/numRecsDown);
xmin = uint16(xmin);
ymin = uint16(ymin);
width = uint16(width);
height = uint16(height);
s = 1;
for i = 1: numRecsDown
for j = 1: numRecsAcross
block = theImage(ymin+i-i:height*i, xmin+j-j:width*j, :);
color = uint8(mean(mean(block)));
rr = color(:,:,1);
gg = color(:,:,2);
bb = color(:,:,3);
RedChannel = 255 * uint8(theGrayImage);
BlueChannel = 255 * uint8(theGrayImage);
GreenChannel = 255 * uint8(theGrayImage);
r = ((double(rr))/255) * RedChannel;
g = ((double(gg))/255) * GreenChannel;
b = ((double(bb))/255) * BlueChannel;
rgbImage = cat(3, r, g, b);
subplot(numRecsAcross,numRecsAcross,s);
imshow(rgbImage);
s = s+1;
end
end
Risposta accettata
Più risposte (2)
Iddo Weiner
il 8 Dic 2016
Try the following trick - instead of specifying the "real" exact number of subplots you need, specify a greater number and then for the 3rd input of subplot() specify a range instead of a number.
Example - say I want to plot 4 panels, and want smaller margins between panels. So instead of going:
subplot(2,2,1)
...
subplot(2,2,4)
I'll define: subplot(M,N,Xi) - where M,N are the dimensions of the plot (and I will suggest setting them to an integer larger than 2), and Xi is a vector defining the location of subplot i. Here's an example in which M=N=4 (data is random):
figure
subplot(4,4,[1,2,5,6])
bar(normrnd(0,1,100,1))
subplot(4,4,[3,4,7,8])
histogram(normrnd(0,1,100,1))
subplot(4,4,[9,10,13,14])
bar(normrnd(0,1,100,1))
subplot(4,4,[11,12,15,16])
histogram(normrnd(0,1,100,1))
Now, you can play around with M and N (don't forget to adjust the range accordingly). This won't actually change the absolute dimensions of the whole figure but it will get rid of the margin space between panels. Of course - the bigger M,N are - the less margin you'll have..
Hope this helps
3 Commenti
Francisco Rogel
il 8 Dic 2016
Iddo Weiner
il 8 Dic 2016
Really straightforward - it's the same way this works for any array in matlab: (1,1) is 1, (1,2) is 2 and so on.. If you specify a range, the subplot will stretch on all values in that range, thus minimizing the grey fraction between panels.
Run the example I posted for visual clarification
Jiro Doke
il 8 Dic 2016
There's also a similar example in the documentation:
The best (and possibly only) way to do that would be to create a new, larger image where you combine all your smaller subplots in the order that you need. For example, say you had four images A,B,C and D all of size m-by-n and you wanted them in a grid, then you would create a new image X of size 2m-by-2n and pass values as:
X(1:m, 1:n) = A;
X(1:m, n+1 : 2*n) = B;
X(m+1 : 2*m, 1:n) = C;
X(m+1 : 2*m, n+1 : 2*n) = D;
and then plot X.
4 Commenti
Francisco Rogel
il 8 Dic 2016
Chaya N
il 8 Dic 2016
You would need to know how many subplots you have and their individual sizes (I am assuming here that they are all the same size). Then you can go in and create a bigger image as in my example above.
Image Analyst
il 8 Dic 2016
Francisco, Chaya is stitching the images together, to create a much bigger image, and displaying them in one axes, not multiple ones with subplots. The line he left off was this
imshow(X)
You could also do
bigImage = [A,B;C,D]
imshow(bigImage);
which is essentially what he did.
Jiro Doke
il 8 Dic 2016
In your code you can do the following. I'm just including code at the end.
rgbImage = cat(3, r, g, b);
acrossImages{j} = rgbImage;
s = s+1;
end
downImages{i} = cat(2,acrossImages{:});
end
bigImage = cat(1,downImages{:});
imshow(bigImage)
Categorie
Scopri di più su Subplots in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!