运用Python的CV2库检测tiff图形中的圆形

运用Python的CV2库检测tiff图形中的圆形

如何确定实验数据的圆形?

实验数据保存为Tiff文件,其实就是一个强度分布的矩阵,与实际的图片是有区别的。

标签图像文件格式(Tag Image File Format,TIFF)是一种灵活的位图格式,主要用来存储包括照片和艺术图在内的图像,最初由Aldus公司与微软公司一起为PostScript打印开发。TIFF与JPEGPNG一起成为流行的高位彩色图像格式。TIFF格式在业界得到了广泛的支持,如Adobe公司的Photoshop、The GIMP Team的GIMP、Ulead PhotoImpact和Paint Shop Pro等图像处理应用、QuarkXPressAdobe InDesign这样的桌面印刷和页面排版应用,扫描、传真、文字处理、光学字符识别和其它一些应用等都支持这种格式。从Aldus获得了PageMaker印刷应用程序的Adobe公司控制着TIFF规范。

我们需要一个检测圆的算法来实现,我主要参考了这两个教程
Detecting Circles in Images using OpenCV and Hough Circles
Circle Detection using OpenCV | Python

但是我需要再做更改的是:

  • 上面给的例子都是导入一个颜色图,具有RGB三个通道的数据,我们用cv2库可以轻松实现不同颜色通道的表示方法。

  • 实验数据是tiff文件,其实是单通道的数据,我们需要先读取数据

np.array(plt.imread(Path+Filename))

再将数据保存为uint8格式

img = img.astype(np.uint8)

再保存为灰度图像数据

gray = np.array(Image.fromarray(img))

再进行锐化处理

gray_blurred = cv2.blur(gray, (3, 3))

最后运用函数cv2.HoughCircles来检测

    detected_circles = cv2.HoughCircles(gray_blurred,
                                        cv2.HOUGH_GRADIENT, dp=20, minDist=400, param1=50,
                                        param2=30, minRadius=60, maxRadius=90)

可以定义一个函数完成这些操作。

def find_center(image_ori, num_approxi):
    num_size_ori = np.shape(image_ori)[0]
    num_size_ori_half = int(num_size_ori/2)

    # First approximated range of the picture
    num_approxi = 200
    img = image_ori[num_size_ori_half-num_approxi:num_size_ori_half +
                    num_approxi, num_size_ori_half-num_approxi:num_size_ori_half+num_approxi]

    img = img.astype(np.uint8)
    gray = np.array(Image.fromarray(img))

    # Blur using 3 * 3 kernel.
    gray_blurred = cv2.blur(gray, (3, 3))
    #gray_blurred =gray
    # Apply Hough transform on the blurred image.
    detected_circles = cv2.HoughCircles(gray_blurred,
                                        cv2.HOUGH_GRADIENT, dp=20, minDist=400, param1=50,
                                        param2=30, minRadius=60, maxRadius=90)
    # if detected_circles is not None:

    #     # Convert the circle parameters a, b and r to integers.
    #     detected_circles = np.uint16(np.around(detected_circles))

    #     for pt in detected_circles[0, :]:
    #         a, b, r = pt[0], pt[1], pt[2]

    #         # Draw the circumference of the circle.
    #         cv2.circle(img, (a, b), r, (0, 255, 0), 2)

    #         # Draw a small circle (of radius 1) to show the center.
    #         cv2.circle(img, (a, b), 1, (0, 0, 255), 3)
    #         cv2.imshow("Detected Circle", img)
    #         cv2.waitKey(0)
    return detected_circles

效果图为

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-pzpXGAcV-1612590186410)(https://raw.githubusercontent.com/knifelees3/my_pictures/master/picgoup/image-20201224173107821.png)]

发现效果还是可以的,自己还是得微调才行。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值