【问题标题】:Identify region of image python识别图像python的区域
【发布时间】:2021-10-10 20:18:56
【问题描述】:

我有一个microscopy image,需要计算红色区域。想法是构建一个函数,返回右图红线内的区域(浮点值,X mm²)。

由于我几乎没有图像处理方面的经验,所以我不知道如何解决这个(也许很愚蠢)问题,所以我正在寻求帮助。其他图像示例非常相似,只有 1 个靠近中心的聚集“兴趣区域”。

我很擅长用 python 编码,并且使用 ImageJ 软件有一段时间了。

任何 python 包、软件、参考书目等都应该有帮助。

谢谢!

编辑: 我手动制作的红色示例只是为了让人们了解我想要什么。检测“兴趣区域”必须在代码内部完成。

【问题讨论】:

  • 库推荐或一般程序设计帮助不是本网站的重点,本网站只是为了获得您已经编写的代码的帮助。
  • 这是什么秘密?线条总是垂直的吗?或者他们可以在不同的角度?你还有其他例子吗?

标签: python image-processing cluster-analysis detection area


【解决方案1】:

Canny、形态变换和轮廓可以提供不错的结果。

虽然它可能需要根据输入图像进行一些微调。

import numpy as np
import cv2 


# Change this with your filename
image = cv2.imread('test.png', cv2.IMREAD_GRAYSCALE)

# You can fine-tune this, or try with simple threshold
canny = cv2.Canny(image, 50, 580)  

# Morphological Transformations
se = np.ones((7,7), dtype='uint8')
image_close = cv2.morphologyEx(canny, cv2.MORPH_CLOSE, se)

contours, _ = cv2.findContours(image_close, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

# Create black canvas to draw area on
mask = np.zeros(image.shape[:2], np.uint8)

biggest_contour = max(contours, key = cv2.contourArea)
cv2.drawContours(mask, biggest_contour, -1, 255, -1)

area = cv2.contourArea(biggest_contour)

print(f"Total area image: {image.shape[0] * image.shape[1]} pixels")
print(f"Total area contour: {area} pixels")

cv2.imwrite('mask.png', mask)
cv2.imshow('img', mask)
cv2.waitKey(0)

# Draw the contour on the original image for demonstration purposes
original = cv2.imread('test.png')
cv2.drawContours(original, biggest_contour, -1, (0, 0, 255), -1)
cv2.imwrite('result.png', original)
cv2.imshow('result', original)
cv2.waitKey(0)

代码产生以下输出:

Total area image: 332628 pixels
Total area contour: 85894.5 pixels

剩下要做的就是将像素转换为您喜欢的测量值。

下面两张图片用于演示。

Result

Mask

【讨论】:

  • 这个解决方案正是我正在寻找的,现在将更改参数以满足我的需要。谢谢,@neatconda!
【解决方案2】:

我对这个话题了解不多,但这似乎是一个与这个问题非常相似的问题,看起来有两个很好的答案:

Python: calculate an area within an irregular contour line

【讨论】:

  • 感谢您的回答,问题和主要区别在于,在我的情况下,我没有轮廓线,我需要识别并创建它。我手动制作的红色示例只是为了让人们了解我想要什么,将更新问题以进行澄清。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-05-13
  • 2021-06-14
  • 2016-08-20
  • 1970-01-01
  • 2021-04-24
  • 2017-08-27
  • 1970-01-01
相关资源
最近更新 更多