【问题标题】:Rotate image based in centroid using the rotation matrix使用旋转矩阵基于质心旋转图像
【发布时间】:2021-12-03 03:14:52
【问题描述】:

我发现这个article 是这个家伙使用质心旋转了一个鸡蛋图像。 我在 OTSU 图像中找到了质心:

_, 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")

研究建议使用旋转矩阵使用质心旋转图像:

你能帮我如何使用opencv使用旋转矩阵吗?

这是我目前的方法,最近我找到了质心:

输入图像:

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")

【问题讨论】:

标签: python opencv image-processing image-rotation


【解决方案1】:

您可以在 OpenCV 中使用包括warpAffinewarpPerspective 在内的warp 函数。 warpAffine(仿射变换)似乎足以满足您的应用程序。无论如何,您可以简单地使用以下代码:

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());

【讨论】:

    猜你喜欢
    • 2022-09-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    • 2018-04-26
    • 1970-01-01
    相关资源
    最近更新 更多