首先获取质心。参考来源
源码案例:
#coding=utf-8
import cv2
import numpy as np
img01="S:\\AdobeppPS\\SKOO\\cc23.jpg"
img = cv2.imread(img01)
# convert image to grayscale image
gray_image = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# convert the grayscale image to binary image
ret,thresh = cv2.threshold(gray_image,127,255,0)
# calculate moments of binary image
M = cv2.moments(thresh)
# calculate x,y coordinate of center
cX = int(M["m10"] / M["m00"])
cY = int(M["m01"] / M["m00"])
# put text and highlight the center
cv2.circle(img, (cX, cY), 5, (255, 255, 255), -1)
cv2.putText(img, "centroid", (cX - 25, cY - 25),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
# display the image
cv2.imshow("Image", img)
cv2.waitKey(0)
Image中有多个blob,使用findContours来查找图像中的轮廓数量并找到每个轮廓的中心。然后再计算几何矩。
要找到blob的质心,执行以下步骤具体是:
1.将图像转换为灰度图。
2.对图像执行二值化。
3.计算图像矩后找到图像的中心。
单个blob的质心寻找。pch为预编译文件
参考官网:https://learnopencv.com/find-center-of-blob-centroid-using-opencv-cpp-python/
位置:
图像矩HU矩
图像像素强度的加权平均值HU矩
实现匹配(通过CV库自带函数)
py源码如下:
import cv2
def main():
img01="S:\\AdobeppPS\\SKOO\\cc23.jpg"
img02="S:\\AdobeppPS\\SKOO\\cc25.jpg"
img03="S:\\AdobeppPS\\SKOO\\cc8.jpg"
im1 = cv2.imread(img01,cv2.IMREAD_GRAYSCALE)
im2 = cv2.imread(img02,cv2.IMREAD_GRAYSCALE)
im3 = cv2.imread(img03,cv2.IMREAD_GRAYSCALE)
m1 = cv2.matchShapes(im1,im2,cv2.CONTOURS_MATCH_I2,0)
m2 = cv2.matchShapes(im1,im3,cv2.CONTOURS_MATCH_I2,0)
m3 = cv2.matchShapes(im2,im3,cv2.CONTOURS_MATCH_I2,0)
print("Shape Distances Between \n-------------------------")
print(format(m1,".6f"))
print(format(m2,".6f"))
print(format(m3,".6f"))
if __name__ == "__main__":
main()
参考来源1:https://blog.csdn.net/LuohenYJ/article/details/88603274