Rotate a spot in a binary image by 45/-45 degree
Mostrar comentarios más antiguos
Hey Community!
I was wondering how I can rotate a specific rect / object in a binary image.
Let me explain!
At first I have a black matrix with an example size of 7x20. I put some ones into it like:
% img => 7x20 logical
midlerow = 4;
midlecol = 10;
witdh = 2;
whalf = 2/2;
hight = 6;
hhalf = 6/2;
img( (midlerow-whalf):(midlerow+whalf), (midlecol-hhalf):(midlecol+hhalf) ) = 1;
Lets say i the matrix looks like this afterwards:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
So the ones represent a white spot in a black image - looking like a recangular
Is there a way to rotate only this area consisting of ones, just by either 45/-45 or 135 / - 135 degree? Like a automatic way that is similar to my code?
As a furhter example:
I rotate by 90/-90 degree as follows:
img( (midlerow-hhalf):(midlerow+hhalf), (midlecol-whalf):(midlecol+whalf) ) = 1;
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0
Thanks & best regards!
/edit: it should also work in simulink
1 comentario
darova
el 29 de Abr. de 2020
Maybe imrotate will serve
Respuesta aceptada
Más respuestas (1)
Image Analyst
el 29 de Abr. de 2020
Simple? No. When you rotate, your canvass enlarges. So you need to first say if you want the canvass to enlarge or do you want to clip corners that pivot out of the original image boundaries. Then you need to say what your center of rotation is. imrotate() doesn't let you specify center of rotation so you'll have to do it yourself with the rotation matrix (look it up on Wikipedia). Then you'll have to paste it back onto the canvass at the right location. You'd have to figure out what the bounding box is after you make sure that your rotation point is not shifted. It's somewhat easier if you have a binary image (0 and 1) because you can use the 'Image' and 'Centroid' properties of regionprops to get the bounding box and centroid.
Here's a start:
props = regionprops(binaryImage, 'Image', 'Centroid');
for k = 1 : length(props)
thisImage = props(k).Image;
xCenter = props(k).Centroid(1);
yCenter = props(k).Centroid(2);
rotatedImage = imrotate(thisImage, 135, 'nearest', 'loose');
% Now paste it back on.
% However it gets tricky if the blob rotated out of the bounds of the original image!!!
end
Categorías
Más información sobre Computer Vision Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








