Split image using a predefined line

Is there anyway to split an image into two using a predefined line that passes through an object's centroid. I have already found the centroid and plotted the line, but I am unsure of how to separate the image using this line. Many Thanks

2 comentarios

Matt J
Matt J el 2 de Mayo de 2014
Editada: Matt J el 2 de Mayo de 2014
Split it into two what? You mean you want a version of the image with the pixels to one side of the line made dark? How would you like to deal with boundary pixels that lie partially on both sides of the line?
Ben
Ben el 2 de Mayo de 2014
Editada: Image Analyst el 2 de Mayo de 2014
I have drawn this line, and i wish to separate the image so i can calculate the height and width of both the bottom and top sections. The image doesn't need to be physically separated, just recognised as two different objects

Iniciar sesión para comentar.

 Respuesta aceptada

Image Analyst
Image Analyst el 2 de Mayo de 2014
Sure. I'm going to assume the line cuts across the entire image. Then use poly2mask() to create a binary image.
mask = poly2mask(x, y);
Then you can either blacken above/below the line.
imageAbove = grayImage;
imageAbove(mask) = 0;
imageBelow = grayImage;
imageBelow(~mask) = 0;
You can also crop to the bounding box or leave it at the original size, using imcrop() or regular indexing.

5 comentarios

Ben
Ben el 2 de Mayo de 2014
Editada: Ben el 2 de Mayo de 2014
this may seem like a stupid question, but what values do i use for the x and y values? my centroid co-ordinates? or my line co-ordinates? But thank you ever so much!
Image Analyst
Image Analyst el 2 de Mayo de 2014
You need to extend the line from the centroid, at whatever angle it is, until each end strikes the boundary of the image. Then add points at the corners of the image. So you will have either 3, 4, or 5 vertices depending on exactly where the line strikes the outer perimeter of your image.
If the line is horizontal, just use normal, regular indexing:
topPart = grayImage(1:middleRow,:);
bottomPart = grayImage(middleRow+1:end, :);
Ben
Ben el 3 de Mayo de 2014
Editada: Ben el 3 de Mayo de 2014
I have a line drawn throught the cetnroid, but i do not know how to stop the line when it reaches the image border. Edit: I only have Matlab 2012 - if that changes anything
Image Analyst
Image Analyst el 3 de Mayo de 2014
Ben, it's just simple algebra. I simply used the point slope formula for a line. See attached test3.m file to produce the image below.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

Ben
el 2 de Mayo de 2014

Comentada:

el 3 de Mayo de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by