opencv Python中图像处理确定结构面积

vyu0f0g1  于 8个月前  发布在  Python
关注(0)|答案(1)|浏览(66)

我正在做一个Python项目,我需要从给定的图像中确定结构的面积。
以下是我到目前为止所做的:
1.读图像。
1.将图像转换为灰度。
1.使用大津方法对灰度图像进行二值化。
1.在结构周围绘制轮廓。
1.用绿色填充封闭区域,以表示确定的区域。
然而,我注意到并没有捕获整个结构;通常,标记的区域比它应该的要小。
我想知道是否有人遇到了类似的挑战,并可以提供指导。也许有一个替代方法或调整我目前的方法,可以产生更好的结果?任何见解或建议将不胜感激。
输入图像:

输出图像:

wr98u20j

wr98u20j1#

import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# Image path
path= "/Users/gauravsingh/Downloads/gxETv.png"

# read the image
image = cv.imread(path, cv.IMREAD_COLOR)

# Total Area of the image
_, h, w = image.shape
totalArea= h*w

# Convert the image to grayscale.
imageGray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

# improve contrast of the image using histogram equalisation.
imageEqu= cv.equalizeHist(imageGray)

# Threshold the image to separate background and foreground
_, imageThresh= cv.threshold(imageEqu, 130, 255, cv.THRESH_BINARY_INV)

# Remove small white pixels outside the structure
imageErode = cv.erode(imageThresh, (3, 3), iterations= 2)

# Count NonZero pixels in the image
NonZeroPixels = cv.countNonZero(imageErode)

# area of structure
structureArea = (NonZeroPixels/ totalArea)
print(f"Area of the Structure is: {structureArea}%")

# Display the image
plt.imshow(imageErode, cmap= 'gray')
plt.show()

相关问题