Chart regions of pixels onto new image
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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.
0 comentarios
Respuestas (2)
jonas
el 19 de Jul. de 2018
Editada: jonas
el 19 de Jul. de 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 comentarios
jonas
el 26 de Jul. de 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.
Ariel Avshalumov
el 26 de Jul. de 2018
1 comentario
jonas
el 26 de Jul. de 2018
Editada: jonas
el 26 de Jul. de 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.
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!