0

I found this article were this guys rotated one egg image using the centroid. I found the centroid based in the OTSU image:

_, thr = cv2.threshold(grayscale, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
moment = cv2.moments(thr)
if moment['m00'] !=0 :
    centroid = (int(moment['m10'] / moment['m00']), int(moment['m01'] / moment['m00']))
    cv2.circle(result, centroid, 3, (0, 255, 0), -1)
    showImage(result, "Centroid")

enter image description here

The reasearches recomends use the rotation matrix to rotate the image using the centroid:

enter image description here

Could you help me how use the rotation matrix using opencv?

This is my current method, recently I found the centroid:

Input images:

enter image description here

enter image description here

def showImage(img, titulo):
    plt.figure(figsize=(5,5))
    plt.title(titulo)
    plt.imshow(img)
    plt.show()

# Segmentar
imagePath = "images/sample1.jpeg"
image = cv2.imread(imagePath)
# convertir de formato BGR a RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
original = image
# Mostrar imagen original
showImage(image, "Imagen Original")

#Aplicar filtro paso bajo (blur)
image = cv2.blur(image,(9,9),0)
    
# Convertir a HSV:
hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
# Mostrar canal R el cual el huevo presenta mayor contraste
showImage(hsv, "hsv image")

# Convertir escala de grises el hsv
grayscale = cv2.cvtColor(hsv, cv2.COLOR_RGB2GRAY)
showImage(grayscale, "Escala de grises hsv")

# Aplicar binarización OTSU
_, thr = cv2.threshold(grayscale, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

showImage(thr, "OTSU imagen")

# Poner máscara thr a la imágen original

result = cv2.bitwise_and(original, original, mask=thr)

showImage(result, "Imagen segmentada")

# Buscar centroide
moment = cv2.moments(thr)
if moment['m00'] !=0 :
    centroid = (int(moment['m10'] / moment['m00']), int(moment['m01'] / moment['m00']))
    cv2.circle(result, centroid, 3, (0, 255, 0), -1)
    showImage(result, "Centroid")
  • 1
    Your question reminds me the following [post](https://stackoverflow.com/questions/67554673/how-can-i-draw-line-for-an-image-in-python). I think the solution may be applied to your problem. – Rotem Dec 03 '21 at 09:15
  • 1
    See https://en.wikipedia.org/wiki/Image_moment It describes the formula for the rotation in terms of moments – fmw42 Dec 03 '21 at 17:39
  • See also https://www.semanticscholar.org/paper/A-survey-of-moment-based-techniques-for-unoccluded-Prokop-Reeves/cb42fe58273bbc8f39fd9064f5e8732b739a0139 – fmw42 Dec 03 '21 at 17:55
  • Thanks dear @Rotem for share your work. Also fmw42 thanks so much for share that reference about image moments. I will do. – FreddicMatters Dec 04 '21 at 00:11

1 Answers1

0

You can use the warp functions including warpAffine and warpPerspective in OpenCV. It seems that the warpAffine (affine transform) would be enough for your application. Anyway, you could simply use the below piece of code:

Point center = Point( image.cols/2, image.rows/2 );
Mat rot_mat = getRotationMatrix2D( center, angle, scale ); // rot_mat is 2x3 double matrix), you can set scale=1
Mat warp_rotate_dst;
warpAffine( image, warp_rotate_dst, rot_mat, image.size());
Mahdi
  • 141
  • 1
  • 4