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()
1条答案
按热度按时间wr98u20j1#