97 lines
2.5 KiB
Python
97 lines
2.5 KiB
Python
import os
|
|
import sys
|
|
import cv2
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
def main():
|
|
lstone=list()
|
|
lsttwo=list()
|
|
image1 = cv2.imread("D:\\MACH\\MACH\\LAB_2\\image0.jpg")
|
|
image2=image1
|
|
image3=image1
|
|
image4=image1
|
|
image5=image1
|
|
image6=image1
|
|
image7=image1
|
|
image2=erode_cv(image2,11)
|
|
image3=dilate_cv(image3,11)
|
|
image4=median_cv(image4,5)
|
|
image5=erode_ipp(image5,14)
|
|
image6=dilate_ipp(image6,14)
|
|
image7=median_ipp(image7,7)
|
|
|
|
|
|
x = np.linspace(-5, 2, 100) # от -5 до 2 сделать 100 точек
|
|
# показать рисунок
|
|
|
|
|
|
|
|
|
|
lstone=sravit_metod(image2)
|
|
lsttwo=sravit_metod(image5)
|
|
lsttree=lsttwo[len(lstone):]
|
|
y1 =len(lstone)*x
|
|
y2 =len(lsttree)*x
|
|
|
|
|
|
lstone=sravit_metod(image3)
|
|
lsttwo=sravit_metod(image5)
|
|
|
|
y3 =len(lstone)*x
|
|
y4 =len(lsttwo)*x
|
|
|
|
lstone=sravit_metod(image4)
|
|
lsttwo=sravit_metod(image7)
|
|
lsttree=lstone[len(lsttwo):]
|
|
y5 =len(lstone)*x
|
|
y6 =len(lsttwo)*x
|
|
|
|
fig, ax = plt.subplots()
|
|
ax.plot(x, y1, color="blue", label="erode_cv")
|
|
ax.plot(x, y2, color="red", label="erode_ipp")
|
|
ax.plot(x, y3, color="green", label="dilate_cv")
|
|
ax.plot(x, y4, color="yellow", label="dilate_ipp")
|
|
ax.plot(x, y5, color="orange", label="median_cv")
|
|
ax.plot(x, y6, color="grey", label="median_ipp")
|
|
ax.set_xlabel("x")
|
|
ax.set_ylabel("y")
|
|
ax.legend()
|
|
plt.show()
|
|
|
|
def erode_cv(s1,s2):
|
|
s1 = cv2.erode(s1, np.ones((s2, s2)))
|
|
return s1
|
|
def dilate_cv(s1,s2):
|
|
s1 = cv2.dilate(s1, np.ones((s2, s2)))
|
|
return s1
|
|
def median_cv(s1,s2):
|
|
s1 = cv2.medianBlur(s1,s2)
|
|
return s1
|
|
def erode_ipp(s1,s2):
|
|
s1 = cv2.erode(s1, np.ones((s2, s2)))
|
|
return s1
|
|
def dilate_ipp(s1,s2):
|
|
s1 = cv2.dilate(s1, np.ones((s2, s2)))
|
|
return s1
|
|
def median_ipp(s1,s2):
|
|
s1 = cv2.medianBlur(s1,s2)
|
|
return s1
|
|
|
|
def sravit_metod(s1):
|
|
cv2.imwrite("im1.jpg",s1)
|
|
s1 = cv2.imread("im1.jpg", 0)
|
|
th, threshed = cv2.threshold(s1, 100, 255,cv2.THRESH_BINARY_INV|cv2.THRESH_OTSU)
|
|
cnts = cv2.findContours(threshed, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[-2]
|
|
t1= 3
|
|
t2 = 20
|
|
xcnts = []
|
|
for cnt in cnts:
|
|
if t1<cv2.contourArea(cnt) <t2:
|
|
xcnts.append(cnt)
|
|
os.remove("im1.jpg")
|
|
return xcnts
|
|
|
|
if __name__=="__main__":
|
|
main()
|
|
|