193 lines
6.3 KiB
Python
193 lines
6.3 KiB
Python
import cv2 as cv
|
|
import math
|
|
import numpy as np
|
|
import os, sys
|
|
|
|
img_cache = {}
|
|
hist_rgb_cache = {}
|
|
|
|
|
|
def readImage(img_path):
|
|
global img_cache
|
|
|
|
if img_path in img_cache.keys():
|
|
pass
|
|
|
|
else:
|
|
# read image
|
|
img_test_rgb = cv.imread(img_path)
|
|
# convert to gray scale
|
|
img_test_rgb = cv.resize(img_test_rgb, (1024, 1024))
|
|
img_cache[img_path] = img_test_rgb
|
|
|
|
return img_cache[img_path]
|
|
|
|
|
|
def getCountourDiff():
|
|
pass
|
|
|
|
|
|
def checkCountourImg1(img1):
|
|
try:
|
|
kernel = cv.getStructuringElement(cv.MORPH_RECT, (6, 6))
|
|
|
|
# calculation for the area of a circle in img1
|
|
heightCenter1 = img1.shape[0] / 2
|
|
widthCenter1 = img1.shape[1] / 2
|
|
heightCircle1 = img1.shape[0] / 2.3
|
|
widthCircle1 = img1.shape[1] / 2.7
|
|
|
|
center1 = (round(widthCenter1), round(heightCenter1))
|
|
axesLength1 = (round(widthCircle1), round(heightCircle1))
|
|
circleArea1 = round(math.pi * widthCircle1 * heightCircle1)
|
|
|
|
# calculation for contours and total area of img1
|
|
L, U, V = cv.split(cv.cvtColor(img1, cv.COLOR_BGR2LUV))
|
|
|
|
channel1 = cv.merge([U, U, U])
|
|
channel1 = cv.cvtColor(channel1, cv.COLOR_BGR2GRAY)
|
|
closed1 = cv.morphologyEx(channel1, cv.MORPH_CLOSE, kernel)
|
|
closed1 = cv.medianBlur(closed1, 3)
|
|
retval, threshold1 = cv.threshold(closed1, 100, 255, cv.THRESH_BINARY)
|
|
|
|
contours1, hierarchy1 = cv.findContours(threshold1, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
|
|
|
|
# cv.drawContours(img1, contours1, -1, (0, 255, 0), 2)
|
|
# cv.imshow("Contours Image 1", img1)
|
|
# cv.waitKey(0)
|
|
# cv.destroyAllWindows()
|
|
# sys.exit()
|
|
|
|
return (contours1, circleArea1)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
def checkCountourImg2(img2):
|
|
kernel = cv.getStructuringElement(cv.MORPH_RECT, (6, 6))
|
|
|
|
try:
|
|
# calculation for the area of a circle in img2
|
|
img2copy = img2.copy()
|
|
|
|
height, width, _ = img2.shape
|
|
heightCenter2, widthCenter2 = height / 2, width / 2
|
|
heightCircle2, widthCircle2 = height / 2.3, width / 2.5
|
|
|
|
center2 = (round(widthCenter2), round(heightCenter2))
|
|
axesLength2 = (round(widthCircle2), round(heightCircle2))
|
|
circleArea2 = round(math.pi * widthCircle2 * heightCircle2)
|
|
|
|
img2circle = cv.ellipse(img2copy, center2, axesLength2, 0, 0, 360, (0, 0, 255), 2)
|
|
|
|
# calculation for contours and total area of img2
|
|
L, U, V = cv.split(cv.cvtColor(img2circle, cv.COLOR_BGR2LUV))
|
|
channel2 = cv.merge([U, U, U])
|
|
channel2 = cv.cvtColor(channel2, cv.COLOR_BGR2GRAY)
|
|
closed2 = cv.morphologyEx(channel2, cv.MORPH_CLOSE, kernel)
|
|
closed2 = cv.medianBlur(closed2, 3)
|
|
retval, threshold2 = cv.threshold(closed2, 110, 255, cv.THRESH_BINARY)
|
|
|
|
contours2, hierarchy2 = cv.findContours(threshold2, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
|
|
|
|
return (contours2, circleArea2)
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
def getCountour(img1, img2):
|
|
try:
|
|
kernel = cv.getStructuringElement(cv.MORPH_RECT, (6, 6))
|
|
totalArea1 = 0
|
|
totalArea2 = 0
|
|
|
|
# calculation for the area of a circle in img1
|
|
heightCenter1 = img1.shape[0] / 2
|
|
widthCenter1 = img1.shape[1] / 2
|
|
heightCircle1 = img1.shape[0] / 2.3
|
|
widthCircle1 = img1.shape[1] / 2.7
|
|
|
|
center1 = (round(widthCenter1), round(heightCenter1))
|
|
axesLength1 = (round(widthCircle1), round(heightCircle1))
|
|
circleArea1 = round(math.pi * widthCircle1 * heightCircle1)
|
|
|
|
# calculation for contours and total area of img1
|
|
L, U, V = cv.split(cv.cvtColor(img1, cv.COLOR_BGR2LUV))
|
|
channel1 = cv.merge([U, U, U])
|
|
channel1 = cv.cvtColor(channel1, cv.COLOR_BGR2GRAY)
|
|
closed1 = cv.morphologyEx(channel1, cv.MORPH_CLOSE, kernel)
|
|
closed1 = cv.medianBlur(closed1, 3)
|
|
retval, threshold1 = cv.threshold(closed1, 110, 255, cv.THRESH_BINARY)
|
|
|
|
contours1, hierarchy1 = cv.findContours(threshold1, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
|
|
|
|
for contour in contours1:
|
|
totalArea1 += cv.contourArea(contour)
|
|
|
|
# calculation for the area of a circle in img2
|
|
img2copy = img2.copy()
|
|
|
|
heightCenter2 = img2copy.shape[0] / 2
|
|
widthCenter2 = img2copy.shape[1] / 2
|
|
heightCircle2 = img2copy.shape[0] / 2.3
|
|
widthCircle2 = img2copy.shape[1] / 2.5
|
|
|
|
center2 = (round(widthCenter2), round(heightCenter2))
|
|
axesLength2 = (round(widthCircle2), round(heightCircle2))
|
|
circleArea2 = round(math.pi * widthCircle2 * heightCircle2)
|
|
|
|
img2circle = cv.ellipse(img2copy, center2, axesLength2, 0, 0, 360, (0, 0, 255), 2)
|
|
|
|
# calculation for contours and total area of img2
|
|
L, U, V = cv.split(cv.cvtColor(img2circle, cv.COLOR_BGR2LUV))
|
|
channel2 = cv.merge([U, U, U])
|
|
channel2 = cv.cvtColor(channel2, cv.COLOR_BGR2GRAY)
|
|
closed2 = cv.morphologyEx(channel2, cv.MORPH_CLOSE, kernel)
|
|
closed2 = cv.medianBlur(closed2, 3)
|
|
retval, threshold2 = cv.threshold(closed2, 110, 255, cv.THRESH_BINARY)
|
|
|
|
contours2, hierarchy2 = cv.findContours(threshold2, cv.RETR_LIST, cv.CHAIN_APPROX_NONE)
|
|
|
|
for contour in contours2:
|
|
totalArea2 += cv.contourArea(contour)
|
|
|
|
# calculation of contour area over circle area
|
|
print(totalArea1, circleArea1, totalArea2, circleArea2)
|
|
sys.exit()
|
|
|
|
circleRatio1 = totalArea1 / circleArea1
|
|
circleRatio2 = totalArea2 / circleArea2
|
|
difference = circleRatio2 / circleRatio1
|
|
|
|
return difference
|
|
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
|
|
def getContourDiff(img1_path, img2_path):
|
|
totalArea1 = 0
|
|
totalArea2 = 0
|
|
|
|
img1 = readImage(img1_path)
|
|
img2 = readImage(img2_path)
|
|
|
|
try:
|
|
(contours1, circleArea1) = checkCountourImg1(img1)
|
|
for contour in contours1:
|
|
totalArea1 += cv.contourArea(contour)
|
|
|
|
(contours2, circleArea2) = checkCountourImg2(img2)
|
|
for contour in contours2:
|
|
totalArea2 += cv.contourArea(contour)
|
|
|
|
# calculation of contour area over circle area
|
|
circleRatio1 = totalArea1 / circleArea1
|
|
circleRatio2 = totalArea2 / circleArea2
|
|
difference = circleRatio2 / circleRatio1
|
|
|
|
return difference
|
|
|
|
except Exception as e:
|
|
print(e)
|