Detect resistors using contours

This commit is contained in:
Pavle Portic 2018-03-04 21:23:11 +01:00
parent 888dd291a5
commit 10ec81cc46
4 changed files with 102 additions and 36 deletions

69
3/contours.py Normal file
View File

@ -0,0 +1,69 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © 2018 pavle <pavle.portic@tilda.center>
#
# Distributed under terms of the MIT license.
import cv2 as cv
import numpy as np
def main():
# for i in [3, 7]:
for i in range(1, 11):
filename = 'set/A' + str(i) + '.png'
src = cv.imread(filename)
w, h, c = src.shape
resize_coeff = 0.4
src = cv.resize(src, (int(resize_coeff * h), int(resize_coeff * w)))
box = find_resistor(src)
cv.drawContours(src, [box], 0, (0, 255, 0), 1)
cv.imshow('img', src)
box = sorted(box, key=lambda coord: coord[0])
x = abs(box[0][0] - box[2][0])
y = abs(box[0][1] - box[2][1])
phi = np.arctan2(y, x)
print box
if box[0][1] < box[2][1]:
print 'right', np.degrees(phi)
elif box[0][1] > box[2][1]:
print 'left', np.degrees(phi)
else:
print 'hor', np.degrees(phi)
cv.waitKey()
def find_resistor(img):
img = contrast(img, 1.5)
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
img = cv.medianBlur(img, 7)
img = cv.Canny(img, 100, 150)
kernel = np.ones((9, 9), np.uint8)
img = cv.dilate(img, kernel, 2)
img = cv.erode(img, kernel, 2)
img, contours, hierarchy = cv.findContours(img, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
if len(contours) is not 0:
max_index, max_area = max(enumerate([cv.contourArea(x) for x in contours]), key = lambda x: x[1])
max_contour = contours[max_index]
rect = cv.minAreaRect(max_contour)
return np.int0(cv.boxPoints(rect))
def contrast(img, clipLimit):
img = cv.cvtColor(img, cv.COLOR_BGR2LAB)
l, a, b = cv.split(img)
clahe = cv.createCLAHE(clipLimit, tileGridSize=(8,8))
cl = clahe.apply(l)
img = cv.merge((cl, a, b))
return cv.cvtColor(img, cv.COLOR_LAB2BGR)
if __name__ == "__main__":
main()

View File

@ -6,49 +6,46 @@
#
# Distributed under terms of the MIT license.
"""
@file hough_lines.py
@brief This program demonstrates line finding with the Hough transform
"""
import sys
import math
import cv2 as cv
import numpy as np
def main(argv):
default_file = "set/A6.png"
filename = argv[0] if len(argv) > 0 else default_file
# Loads an image
src = cv.imread(filename, cv.IMREAD_GRAYSCALE)
# Check if image is loaded fine
if src is None:
print ('Error opening image!')
print ('Usage: hough_lines.py [image_name -- default ' + default_file + '] \n')
return -1
dst = cv.Canny(src, 50, 200, None, 3)
def main():
filename = "set/A1.png"
src = cv.imread(filename)
# dst = cv.Canny(src, 50, 200, None, 3)
# Copy edges to the images that will display the results in BGR
cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
edges = cv.Canny(src, 20, 250, apertureSize = 3)
lines = cv.HoughLinesP(edges, 1, np.pi/180, 100, minLineLength=100, maxLineGap=10)
dest = cv.cvtColor(edges, cv.COLOR_GRAY2BGR)
if lines is not None:
# for i in range(0, len(lines)):
for i in [ 0 ]:
rho = lines[i][0][0]
theta = lines[i][0][1]
a = math.cos(theta)
b = math.sin(theta)
x0 = a * rho
y0 = b * rho
pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)
print type(lines)
for line in lines:
x1,y1,x2,y2 = line[0]
# cv.line(dest, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv.imshow("Source", src)
cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
cv.imshow('Detected', dest)
cv.waitKey()
# cdst = cv.cvtColor(dst, cv.COLOR_GRAY2BGR)
# lines = cv.HoughLines(dst, 1, np.pi / 180, 150, None, 0, 0)
# if lines is not None:
# for i in [ 0 ]:
# rho = lines[i][0][0]
# theta = lines[i][0][1]
# a = np.cos(theta)
# b = np.sin(theta)
# x0 = a * rho
# y0 = b * rho
# pt1 = (int(x0 + 1000*(-b)), int(y0 + 1000*(a)))
# pt2 = (int(x0 - 1000*(-b)), int(y0 - 1000*(a)))
# cv.line(cdst, pt1, pt2, (0,0,255), 3, cv.LINE_AA)
# print type(lines)
# cv.imshow("Source", src)
# cv.imshow("Detected Lines (in red) - Standard Hough Line Transform", cdst)
# cv.waitKey()
return 0
if __name__ == "__main__":
main(sys.argv[1:])
main()

Binary file not shown.

Before

Width:  |  Height:  |  Size: 117 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 67 B