Effacer les filtres
Effacer les filtres

Chart regions of pixels onto new image

4 vues (au cours des 30 derniers jours)
Ariel Avshalumov
Ariel Avshalumov le 19 Juil 2018
Modifié(e) : jonas le 26 Juil 2018
I would like to take box or circle portions of an image and create a new image based on them. These regions would be in the same place spatially and any other area not covered by a circle or box would be black (ie pixelValue = [0,0,0]).
Is there any tool or function that can do that on Matlab? I had limited success with imcrop (the pixels weren't preserved spatially), and when I predefined the new image as image = zeros(256,256) the pixels were transcribed correctly spatially but they weren't the correct values (The image integrity wasn't preserved, ie the pixel values weren't correctly transcribed).
The boxes selected would be the regions that aren't white in this example image:
This is the end image
This was the starting image:
Of course, these aren't squares like the ones I'm going for (I wouldn't change their sizes, and they would be squares/circles not rectangles) but I drew this up with paint to try to illustrate what I'm trying to do.

Réponses (2)

jonas
jonas le 19 Juil 2018
Modifié(e) : jonas le 19 Juil 2018
"when I predefined the new image as image = zeros(256,256) ... (The image integrity wasn't preserved, ie the pixel values weren't correctly transcribed)."
This is probably because you prescribed a double and not uint8. Try:
out=zeros(size(RGB),'uint8')
Here's a working example that works for any polygon.
RGB=imread('image13.png');
subplot(1,2,1);
imshow(RGB);hold on
%%Convert to 2D grayscale
GRAY=rgb2gray(RGB);
%%draw pentagram in relative coordinates
xa = [0.5;0.2;1.0;0;0.8;0.5];
ya = [1.0;0.1;0.7;0.7;0.1;1]-0.05;
%%Convert to absolute coordinates and plot shape
x=xa.*size(GRAY,2);
y=ya.*size(GRAY,1);
plot(x,y)
%%Find points inside pentagram
[X,Y] = ind2sub(size(GRAY),1:numel(GRAY))
[in]=inpolygon(Y(:),X(:),x,y);
%%Make new image with pixels outside of the pentagrap white
red=RGB(:,:,1);
green=RGB(:,:,2);
blue=RGB(:,:,3);
red(~in)=255;
green(~in)=255;
blue(~in)=255;
out=zeros(size(RGB),'uint8')
out(:,:,1)=red;
out(:,:,2)=green;
out(:,:,3)=blue;
%%Plot
subplot(1,2,2)
imshow(out)
  7 commentaires
Ariel Avshalumov
Ariel Avshalumov le 26 Juil 2018
My code is a little hard to look at, but I'll try to shorten and make it as presentable as possible.
-
image=img_mat{13} ;
% pulls out image 13 from a cell array of matrices
% this image is 256x256, the size does get changed to fit alexnet, but this is not particularly relevant to the problem.
bounds = [26 200 26 150]; % X1, X2, Y1, Y2 the dimensions of where I want the boxes to go. This is because I need the squares on the foreground of the image and not the background. Not all of these pixels are on the actual foreground.
x=bounds(2)-bounds(1);
% X2 - X1
y=(bounds(4)-bounds(3));
% Y2 - Y1
side=floor(sqrt(x*y/20));
%one side of the square.
hside=floor(side/2);
%Half the size of the square. I am placing the center of the square on the pixel that Matlab is looking on.
boxdimcent=floor([(bounds(2)-bounds(1))/2, (bounds(4)-bounds(3))/2]);
box1=boxdimcent-hside;
%Pick a pixel that Matlab will "focus" on
domain= [box1(1)-10:box1(1)+35];
range= [box1(2)-10:box1(2)+35];
% Variablility of where the box is being picked from.
size = [256 256];
newimg2=zeros(size,'uint8');
%Your additional code.
for n = 1:4
for m = 1:4
%16 possible squares. They are randomly picked in a different piece of code not included here.
left2=[randi([domain(1),domain(11)]),randi([range(1),range(11)])];
posileft2 = [left2(1)-side+(side*(m-1)),left2(2)-side+(side*(n-1)),side,side];
% Position of the pixel that will be the leftmost pixel of the box.
newimg2(posileft2(1):(posileft2(1)+posileft2(3)),posileft2(2):(posileft2(2)+posileft2(4)))=imcrop(image,[posileft2(1),posileft2(2),side,side]);
% !!! The problem is probably here. this places the square that was chosen onto the new image.
% There is error checking here as well to make sure that the program does not pick the same square and reprint it. I have not included it though.
end
end
I know this code isn't amazing, but I am relatively new in Matlab. Also this isn't the real size of the boxes, since I select the sizes from a matrix (just like I select the images). That is why when you see the newimg2 it'll only cover 1/4th of the image but that is not the case in the original code. What I want to know is how to not have the squares map randomly.
jonas
jonas le 26 Juil 2018
No wonder it's not working, you have not used a single line of the code I gave you. If you have already decided on specific method, then include that in the original submission so I don't have to waste my time writing a script from scratch. Nothing I post here will make your original buggy code magically work.
Rant aside, I have fixed some obvious flaws with your code. If you want more help, then I suggest you start a new question.

Connectez-vous pour commenter.


Ariel Avshalumov
Ariel Avshalumov le 26 Juil 2018
Okay. I apologize. I am rather new with matlab and your code is great for 1 image , but I need to do this to a large number of different images with varying characteristics.
That is why I wrote it the way I did, because the boxes have to be differently chosen for each image and they need to meet certain criteria based on their features (and this is one small part of the code for a specific set of these images).
I hope you can understand and I apologize again for taking away so much of your valuable time:
Ariel
  1 commentaire
jonas
jonas le 26 Juil 2018
Modifié(e) : jonas le 26 Juil 2018
Thats ok, we are both here to learn, you by asking and me by answering questions. It's just frustrating sometimes when the question is unclear.
With that said, I do not think my method is any less general than yours. It just takes pixels from one image and puts them in another. Obviously you have to define the polygons yourself, depending on the features of the image (can be random or anything really). How to define the polygons was not clear in the original question, which is what made this whole thread very confusing.

Connectez-vous pour commenter.

Community Treasure Hunt

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

Start Hunting!

Translated by