How to divide an image into 8 equal sectors?
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
anastasia
le 20 Avr 2017
Commenté : Image Analyst
le 24 Avr 2017
I have an image. I need to divide the image into 8 equal sectors as shown below.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/163158/image.jpeg)
I have used the following code:
clear all;
close all;
im=imread('apple.jpg');
r=100;
out1 = ones(max(size(im,1),r*2)+2,max(size(im,2),r*2)+2,3).*255;
xoffset = floor((size(out1,2)-size(im,2))/2);
yoffset = floor((size(out1,1)-size(im,1))/2);
out1(yoffset:yoffset+size(im,1)-1,xoffset:xoffset+size(im,2)-1,:) = im(:,:,:);
im = out1;
cy = floor(size(im,1)/2);
cx = floor(size(im,2)/2);
figure;
imshow(uint8(im));
hold on
pos = [cx-r+1 cy-r+1 r*2 r*2];
rectangle('Position',pos,'Curvature',[1 1]);
x1 = [-r, 0, -r*cosd(45), -r*cosd(45); r, 0, r*cosd(45), r*cosd(45)]+cx+1;
y1 = [0, -r, -r*sind(45), r*sind(45); 0, r, r*sind(45), -r*sind(45)]+cy+1;
plot(x1,y1);
hold off
figure;
for i = 0:45:315
t = linspace(-i,-i-45,128);
x = [cx, cx+r*cosd(t), cx];
y = [cy, cy+r*sind(t), cy];
bw = poly2mask( x, y, size(im,1),size(im,2));
bw = repmat(bw,1,1,3);
out = ones(size(im,1),size(im,2),size(im,3)).*155;
out(bw) = im(bw);
subplot(2,4,(i/45)+1); imshow(uint8(out));
end;
I need to do this for a dataset of over 100 images with different sizes. My problem is that, I have to manually assign the value for r for every image. How can I automatically assign the value for r? Please help. I also need to minimize the background (white parts) in the sectors as much as possible. How do I go about this? Please help. Thank you.
0 commentaires
Réponse acceptée
Image Analyst
le 20 Avr 2017
Just mask the image with sector shapes. See the FAQ:
3 commentaires
Image Analyst
le 24 Avr 2017
Find the apple mask by converting to hsv colorspace with rgb2hsv() then thresholding the S channel. The apple will have S values more than about 0.2. Now you have a mask and you can use it to blacken outside the apple, crop the image, or both.
Plus de réponses (0)
Voir également
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!