Nhận xét, đánh giá

Một phần của tài liệu Ứng dụng dobot magician trong hệ thống phân loại sản phẩm (Trang 79)

Sau thời gian tìm hiểu, nghiên cứu và phát triển hệ thống “Ứng dụng Dobot Magician trong hệ thống phân loại sản phẩm” tuy gặp rất nhiều khó khăn và trở ngại khi thực hiện nhưng cuối cùng nhóm cũng đã đạt được những kết quả khả quan. Mô hình giúp nâng cao hiệu quả sản xuất, kiểm soát chặt chẽ được số lượng và chất lượng sản phẩm, tránh các sai sót chủ quan (do mệt mỏi, do phân loại tính chất tương đối,…) tăng tính tự động hóa thay thế cho con người.

Mặc dù đã rất cố gắng trong quá trình thực hiện, tuy nhiên do giới hạn về kiến thức, trình độ lẫn thời gian thực hiện nên việc thiếu sót là điều không thể tránh khỏi, mong nhận đươc sự đóng góp ý kiến của giảng viên hướng dẫn, giảng viên phản biện và hội đồng bảo vệ để đề tài có thể được hoàn thiện tốt hơn.

63

Khoa ĐT CLC – ĐHSPKT TP.HCM

CHƯƠNG 6. KẾT LUẬN VÀ HƯỚNG PHÁT TRIỂN 6.1 Kết luận

6.1.1 Ưu điểm

- Nhận diện gần như chính xác màu sắc của vật thể, ít bị ảnh hưởng bởi nhiễu do sử dụng thuật toán tìm kiếm màu gần nhất thay vì thuật toán tìm kiếm màu chính xác. Dễ hiểu hơn, khi ta sử dụng thuật toán tìm kiếm màu chính xác thì màu mẫu ban đầu phải giống hoàn toàn so với màu vật thể thực tế. Tuy nhiên, do các tác nhân không mong muốn như ánh sáng môi trường tự nhiên (sáng quá sẽ gây nên tình trạng đổ bóng, tối quá thì màu vật thể bị thay đổi), màu của các khối vật thể không đồng đều với nhau,… sẽ tác động ít nhiều đến màu vật thể thực tế, gây nên tình trạng nhận diện sai màu so với màu mẫu và từ đó hệ thống cũng sẽ hoạt động sai với yêu cầu đề ra ban đầu. Vì vậy giải quyết điều này ta sử dụng thuật toán tìm kiếm màu gần nhất nhằm giảm bớt các tác động tự nhiên không mong muốn tránh tình trạng nhận diện sai màu dẫn đến phân loại sai.

- Các thuật toán nhận diện màu sắc thông thường chỉ có thể nhận diện được màu của một vật thể duy nhất, giả sử trường hợp có hai hay nhiều vật thể cùng màu hay khác màu được đặt sát vào nhau, lúc này thuật toán sẽ không thể giải quyết được vấn đề này gây nên tình trạng lỗi hệ thống, cánh tay Robot từ đó sẽ không thể xác định được đúng tâm vật thể hoặc vật thể sẽ bị trôi ra ngoài băng chuyền. Giải quyết vấn đề này ta áp dụng thêm thuật toán nhận diện biên Canny và lấy cạnh vật thể Houghline nhằm xác định chính xác đang có bao nhiêu vật thể trên băng chuyền, khắc phục được nhược điểm hệ thống không thể phân biệt được nhiều vật thể cùng màu sắc được đặt sát nhau hoặc nhiều vật thể với nhiều màu sắc khác nhau được đặt sát vào nhau.

6.1.2 Nhược điểm

- Nhận diện màu sắc, cạnh và biên vật thể đôi lúc chưa đúng do tác động của ánh sáng môi trường tự nhiên thay đổi (môi trường quá sáng hoặc quá tối) có thể khiến cho vật thể bị đổ bóng, màu sắc vật thể và màu đen của băng tải bị thay đổi.

- Không sử dụng không gian màu HSV để loại bỏ ánh sáng của môi trường tự nhiên. - Độ chính xác hệ thống chưa cao do nhiều nguyên nhân cấu thành: không biết chính xác số xung để quay được một vòng của động cơ băng tải, các khoảng cách được thiết lập vẫn có sai số, các khớp xoay của cánh tay Dobot Magician vẫn độ rơ nhất định do kết cấu phần cứng chưa tốt và độ lặp lại vị trí sau mỗi lần di chuyển lệch ít nhất 0.2mm,...

- Tốc độ hoạt động của hệ thống chưa cao vì băng tải không thể hoạt động đồng thời với cánh tay Robot (cho băng tải chạy liên tục và cánh tay Robot sẽ chạy theo gắp vật thể) do thư viện của nhà sản xuất cung cấp chưa có hàm hỗ trợ.

- Cơ cấu chấp hành không nâng được vật thể nặng (tối đa 500g) và có bề mặt gồ ghề (chỉ nâng được vật thể có bề mặt phẳng).

64

Khoa ĐT CLC – ĐHSPKT TP.HCM

6.1.3 Khó khăn

- Không tìm được thư viện điều khiển Dobot Magician trên trang chủ của nhà sản xuất cung cấp.

=> Gửi mail liên hệ trực tiếp đến nhà sản xuất Dobot.

- Không có thư viện nhận diện hình vuông (nhận diện biên và phát hiện đường thẳng trong ảnh) nguồn mở.

=> Tham khảo các thành viên trên các diễn đàn về thị giác máy tính từ đó nghiên cứu các giải thuật xử lý ảnh (Canny và Houghline) và viết chương trình điều khiển. - Hệ thống không có hệ thống đo lường chuẩn.

=> Chỉ có thể căn chỉnh bằng tay và mắt thường dựa trên cảm tính. - Giới hạn chuyên môn và trình độ học vấn.

=> Học thêm các hàm xử lý nâng cao trong Python và xử lý ảnh. - Không tìm được thông số động cơ băng chuyền.

=> Cấp vào động cơ số lượng xung bất kì và dựa trên cảm tính quan sát sao cho động cơ băng chuyền quay đúng một vòng từ đó tìm được số xung.

- Không nhận diện được biên và cạnh vật thể trong ảnh do tác động của ánh sáng môi trường (quá sáng hoặc quá tối) khiến cho vật thể bị đổ bóng hoặc sai màu sắc.

=> Chạy hàm lấy mẫu màu sắc trước khi vận hành và điều chỉnh độ sáng, độ tương phản trong phần mềm của Camera.

- Đôi lúc cánh tay Robot gắp vật thể xếp vào kho bị lệch góc 450. => Chưa tìm ra nguyên nhân và hướng khắc phục.

6.2 Hướng phát triển

- Cải thiện giải thuật để không chỉ nhận dạng được 5 màu mà còn có thể nhận dạng được rất nhiều màu khác, phân biệt được các màu chung một gốc màu như đỏ đậm, đỏ tươi, đỏ thắm, xanh da trời, xanh lam, xanh cô ban,…

- Hiển thị được thông số của các khớp xoay, vận tốc, gia tốc và momen động cơ khi Robot hoạt động.

- Cải tiến chương trình để hệ thống có thể reset tự động và lặp lại quy trình thay cho phương thức reset thủ công như hiện tại.

- Thay vì sử dụng cánh tay Dobot Magician (chủ yếu được phục vụ cho học tập và nghiên cứu) để gắp thả vật thể trong hệ thống, ta sử dụng cánh tay Robot công nghiệp Dobot M1 (hình 6.1) nhằm cải thiện tốc độ hoạt động và nâng cao độ chính xác cho hệ thống. (adsbygoogle = window.adsbygoogle || []).push({});

65

Khoa ĐT CLC – ĐHSPKT TP.HCM

Hình 6. 1 Dobot M1

- Tìm kiếm giải thuật và nghiên cứu các hàm điều khiển để băng chuyền có thể hoạt động đồng thời với cánh tay Robot, tức là cánh tay Robot sẽ chạy theo gắp các vật thể trên băng chuyền khi băng chuyền đang hoạt động (chạy liên tục không dừng), từ đó nâng cao năng suất hệ thống.

66

Khoa ĐT CLC – ĐHSPKT TP.HCM

TÀI LIỆU THAM KHẢO Tiếng Việt

[1] PGS.TS Nguyễn Trường Thịnh (2014), “Giáo trình Kỹ thuật Robot”, Nhà xuất bản Đại học Quốc gia Thành phố Hồ Chí Minh

[2] TS. Nguyễn Thanh Hải (2014), “Giáo trình Xử lý ảnh”, Nhà xuất bản Đại học Quốc gia Thành phố Hồ Chí Minh

[3] Phạm Trần Lam Hải, Trần Thanh Hùng, Nguyễn Văn Khanh, Lưu Trọng Hiếu (2015), “Ứng dụng xử lý ảnh số trong việc theo dõi sự chuyển động của các Robot di động”, Tạp chí Khoa học Trường Đại học Cần Thơ

[4] Nguyễn Văn Long (2016), “Ứng dụng Xử lý ảnh trong thực tế với thư viện OpenCV”

[5] Võ Đức Khánh, Hoàng Văn Kiếm (2008), “Giáo trình xử lý ảnh số”, Nhà xuất bản Đại học Quốc gia Thành phố Hồ Chí Minh

[6] Nguyễn Tiến Phúc (2017), “Xử lý ảnh trong phân loại sản phẩm theo màu sắc”,

Bài báo nghiên cứu khoa học Trường Đại học Sao Đỏ

[7] Nguyễn Quang Hoan (2006), “Xử lý ảnh”, Lưu hành nội bộ Học viện công nghệ bưu chính viễn thông

[8] Nguyễn Thị Lan (2011), “Tìm hiểu phương pháp BPR cho bài toán tìm xương của ảnh”, Đồ án tốt nghiệp Trường Đại học Dân lập Hải Phòng

[9] Chí Trương (2013), “Một vài khái niệm cơ bản về xử lý ảnh số”, Wordpress

vtct.wordpress.com/2013/06/19/mot-vai-khai-niem-co-ban-ve-anh-so-digital-image/

[10] Việt Anh (2018), “Ảnh số và các không gian màu trong xử lý ảnh”, Blog Việt Anh

blog.vietanhdev.com/posts/computer-vision/2018-09-19-anh-so-va-cac-khong-gian- mau-trong-xu-ly-anh/

[11] Tạ Hải (2017), “Giới thiệu khái quát về ảnh số”, Blog Lập trình Matlab

www.matlabthayhai.info/2015/11/bai-1-gioi-thieu-khai-quat-ve-anh-so.html

[12] Team Việt Dev (2018), “OpenCV là gì? Ứng dụng của nó trong thế giới thực”,

Blog Team Việt Dev

https://teamvietdev.com/opencv-la-gi-ung-dung-opencv-trong-the-gioi-thuc/

[13] Nguyễn Nghĩa (2016), “Một cái nhìn tổng quản về Visual Studio IDE”,

EITGuide

67

Khoa ĐT CLC – ĐHSPKT TP.HCM

Tiếng Anh (adsbygoogle = window.adsbygoogle || []).push({});

[1] Bill Green (2002), “Canny Edge Detection”, DocsOpenCV.com

[2] Krutika Bapat (2019), “Hough Transform with OpenCV”, LearnOpenCV.com

[3] Andreas Richtsfeld, Thomas Morwald, Johann Prankl, Michael Xillich and Markus Vincze (2012), “Segmentation of Unknown Objects in Indoor Environments”, IEEE/RSJ International Conference on Intelligent Robots and Systems

[4] R.B Rusu, A. Holzbach, M. Beetz (2009), “Detecting and Segmenting Objects for Mobile Manipulation”, S3DV Workshop of the 12th International Conference on Computer Vision (ICCV)

[5] Gonzalez, RC and Woods, RE (2008), “Digital Image Processing”, Pearson Prentice Hall, Upper Saddle River

[6] Bhabatosh Chanda, Dwijesh Dutta Maumder (2001), “Digital Image Processing and Analysis”, Prentice Hall of India

[7] Wilhelm Burger and Mark J. Burge (2009), “Principles of Digital Image Processing”, Fundamental Techniques, Sprigner

[8] Support Dobot, “Dobot Magician User Guide”, Dobot.cc

https://download.dobot.cc/product-manual/dobot-magician/pdf/V1.7.0/en/Dobot- Magician-User-Guide-V1.7.0.pdf

[9] Support Dobot, “Dobot Magician Demo Description”, Dobot.cc

https://download.dobot.cc/product-manual/dobot-magician/pdf/en/dobot-magician- demo-description.pdf

[10] Support Dobot, “Dobot Magician API Description”, Dobot.cc

http://download.dobot.cc/development-protocol/dobot-magician/pdf/api/en/Dobot- Magician-API-DescriptionV1.2.3.pdf

68

Khoa ĐT CLC – ĐHSPKT TP.HCM

PHỤ LỤC

Chương trình điều khiển:

Lấy màu mẫu: Color_Sample import cv2

cap = cv2.VideoCapture(0) cap.set(3, 320)

cap.set(4, 240)

while True:

# doc 1 anh tu camera frame = cap.read()[1] with open('specs', 'r') as f: content = f.read() specs = content.split() specs = list(map(int, specs)) f.close()

# cat khung hinh lay phan chua bang bang chuyen

img = frame[frame.shape[0]//3:frame.shape[0]//3*2, 98:-98] red = img[specs[0]:specs[2], specs[1]:specs[3]]

green = img[specs[4]:specs[6], specs[5]:specs[7]] blue = img[specs[8]:specs[10], specs[9]:specs[11]] yellow = img[specs[12]:specs[14], specs[13]:specs[15]]

cv2.imshow("red", red) cv2.imshow("green", green) cv2.imshow("blue", blue) cv2.imshow("yellow", yellow) cv2.imshow("adad", img) if cv2.waitKey(2) & 0xFF == 27: cv2.imwrite("photos/red.png", red)

69 Khoa ĐT CLC – ĐHSPKT TP.HCM cv2.imwrite("photos/green.png", green) cv2.imwrite("photos/blue.png", blue) cv2.imwrite("photos/yellow.png", yellow) break cv2.destroyAllWindows()

Nhận diện hình vuông: Rect_Recognition import cv2

importnumpy as np

from random importrandint (adsbygoogle = window.adsbygoogle || []).push({});

from math import atan, pi, fabs, sqrt, tan

from matplotlib import pyplot

from mpl_toolkits.mplot3dimportAxes3D

rect_width = 31 delta_dir = 10*pi/180

def get_distance_p2l(point, line):

'''/

Calculate distance from point to line @param:

point: coordination of the point format: [x, y]

line: line's index in the list of all lines format: integer

@ return: distance from point to line format: float

'''

a, b, c = get_fomular_of_line(line)

70

Khoa ĐT CLC – ĐHSPKT TP.HCM

def get_distance_l2l(line1, line2):

'''

Calculate distance from line1 to line2 @param:

lineX: a list contains center, direction and lenght of lineX format: [center, direction, lenght]

@return:

distance from line1 to line2 format: float

'''

return (get_distance_p2l(line1[0], line2[:2]) + get_distance_p2l(line2[0], line1[:2]))/2

def get_distance_p2p(p1, p2):

'''

Calculate distance from point 'p1' to point 'p2' @param:

pX: coordination of pX format: float

'''

return sqrt((p1[0]-p2[0])**2+(p1[1]-p2[1])**2)

def get_intersection(line1, line2):

''' (adsbygoogle = window.adsbygoogle || []).push({});

Find intersection's coordinate of line1 nad line2 @param:

lineX: a list contains center, direction and lenght of lineX format: [center, direction, lenght]

@return:

intersection's coordinate of line1 nad line2 format: [x, y]

71 Khoa ĐT CLC – ĐHSPKT TP.HCM a1, b1, c1 = get_fomular_of_line(line1) a2, b2, c2 = get_fomular_of_line(line2) if a1 == 0: y = fabs(c1/b1) x = fabs((y*b2 + c2)/a2) elif a2 == 0: y = fabs(c2/b2) x = fabs((y*b1 + c1)/a1) else: y = fabs((c1-c2)/(b1-b2)) x = fabs((b1*y+c1)/a1) return x, y

def get_center_rect(rect, ls_of_cen_dir_len):

'''

Find center's coordinate of rectangle @param:

rect: a list contains pair of horizontal lines and pair of verticac lines format [[line_v1, line_v2], [line_h1, lin_h2]]

ls_of_cen_dir_len: a list contains center, direction and lenght of all lines format: [[center1, direction1, lenght1], [center2, direction2, lenght2], ...] @return: center's coordinate of rectangle

format: int [x, y] '''

ls_of_peaks = [] for line_v in rect[0]: for line_h in rect[1]:

inter = get_intersection(ls_of_cen_dir_len[line_v], ls_of_cen_dir_len[line_h]) ls_of_peaks.append(inter)

72

Khoa ĐT CLC – ĐHSPKT TP.HCM

y = (ls_of_peaks[0][1] + ls_of_peaks[1][1] + ls_of_peaks[2][1] + ls_of_peaks[3][1])/4 sum_direction = ls_of_cen_dir_len[rect[1][0]][1] + ls_of_cen_dir_len[rect[0][0]][1] + pi/2

sum_direction /= 2

return (int(x), int(y)), sum_direction

def is_between_2lines(points, lines):

'''

Check if points are between 2 lines @param:

points: coordination of points format: float [x, y]

lines: formulars 'ax + by + c = 0' of 2 lines format: float [a, b, c]

@return: boolean value ''' for c in points: if (c[0]*lines[0][0]+c[1]*lines[0][1]+lines[0][2]) * (c[0]*lines[1][0] + c[1]*lines[1][1] + lines[1][2]) >= 0: return False return True def get_fomular_of_line(line): '''

Calculate formular of line @param:

line: a list contains center, direction and lenght of line format: [center, direction, lenght]

@return:

formular of line 'ax + by +c = 0' format: float[a, b, c]

73 Khoa ĐT CLC – ĐHSPKT TP.HCM ''' if line[1] == 0: a = 0.0 b = 1.0 else: a = 1.0 b = a * tan(line[1]+pi/2) c = -(a*line[0][0] + b*line[0][1]) return a, b, c def is_rect(v1, v2, h1, h2): '''

Check if for line v1, v2, h1, h2 are parts of real rect @param: (adsbygoogle = window.adsbygoogle || []).push({});

v1, v2, h1, h2: lists contain center, direction and lenght of v1, v2, h1, h2 format: float[center, direction, lenght]

@return:

boolean value '''

line_v = (get_fomular_of_line(v1), get_fomular_of_line(v2)) center_h = (h1[0], h2[0])

line_h = (get_fomular_of_line(h1), get_fomular_of_line(h2)) center_v = (v1[0], v2[0])

ifnot is_between_2lines(center_h, line_v): return False

elifnot is_between_2lines(center_v, line_h): return False

else: return True

def get_dist_3d_p2p(color, center):

74

Khoa ĐT CLC – ĐHSPKT TP.HCM

def get_nearest_color(color_mean, color_center):

nearest_dist = get_dist_3d_p2p(color_mean, color_center[0]) nearest_color = 0

for idx, center in enumerate(color_center[1:], 1): dist = get_dist_3d_p2p(color_mean, center) if dist < nearest_dist:

nearest_dist = dist nearest_color = idx return nearest_color

def detect_color(ls_of_rects, color_center, img):

# color_ls = ['red', 'green', 'blue', 'yellow', 'black']

for idx, rect in enumerate(ls_of_rects):

rect_img = img[rect[0][1]-10:rect[0][1]+10, rect[0][0]-10:rect[0][0]+10] cv2.imshow("rect", rect_img)

color_mean = rect_img.mean(axis=0).mean(axis=0).astype(int) color_id = get_nearest_color(color_mean, color_center)

rect.append(color_id)

def detect_pair_closed_parallel_lines(cluster, ls_of_cen_dir_len):

'''

Find pairs of close parallel lines in cluster of lines @param:

cluster: a list contains horizontal lines and vertical lines format: [[h1, h2, ...], [v1, v2, ...]]

ls_of_cen_dir_len: a list contains center, direction, lenght of all lines format: float[[center, direction, lenght], ...]

@return: a set contains list of couples vertical lines's index and a list os couples horizontal lines's index

format: '''

75

Khoa ĐT CLC – ĐHSPKT TP.HCM for idx, i in enumerate(cluster[0][:-1]): (adsbygoogle = window.adsbygoogle || []).push({});

for j in cluster[0][idx+1:]:

dist_l2l = get_distance_l2l(ls_of_cen_dir_len[i], ls_of_cen_dir_len[j]) if dist_l2l >= rect_width*0.85 and dist_l2l <= rect_width*1.15: ls_of_couple_v.append((i, j))

ls_of_couple_h = []

for idx, i in enumerate(cluster[1][:-1]): for j in cluster[1][idx+1:]:

dist_l2l = get_distance_l2l(ls_of_cen_dir_len[i], ls_of_cen_dir_len[j]) if dist_l2l >= rect_width*0.85 and dist_l2l <= rect_width*1.15: ls_of_couple_h.append((i, j))

return (ls_of_couple_v, ls_of_couple_h)

def detect_real_rect(ls_of_cen_dir_len, clusters_of_paral_vert_lines):

'''

@param:

ls_of_cen_dir_len: a list contains center, direction, lenght of all lines format: float[[center, direction, lenght], ...]

clusters_of_paral_vert_lines: list of all clusters format: [[[h1, h2, ...], [v1, v2, ...]], ...] @return:

ls_of_real_rects: a list contains center of real rects format: [[(x0, y0), dir0], [(x1, y1), dir1], ...] '''

ls_of_center_rects = [] ls_of_real_rects = []

for cluster in clusters_of_paral_vert_lines:

ls_of_pairs = detect_pair_closed_parallel_lines(cluster, ls_of_cen_dir_len)

for v in ls_of_pairs[0]: for h in ls_of_pairs[1]:

76

Khoa ĐT CLC – ĐHSPKT TP.HCM if is_rect(ls_of_cen_dir_len[v[0]], ls_of_cen_dir_len[v[1]],

ls_of_cen_dir_len[h[0]], ls_of_cen_dir_len[h[1]]):

# check if v and h are parts of a real rect

vh = [v, h]

# compute

center, direction = get_center_rect(vh, ls_of_cen_dir_len)

# cluster duplicated rects into groups

added = False

for i in ls_of_center_rects:

if get_distance_p2p(center, i[0][0]) < rect_width*0.5: i.append((center, direction))

added = True

break (adsbygoogle = window.adsbygoogle || []).push({});

ifnot added:

ls_of_center_rects.append([(center, direction)])

# compute average center of duplicated rect group

for c in ls_of_center_rects: x_sum = 0 y_sum = 0 dir_sum = 0 for each in c: x_sum += each[0][0] y_sum += each[0][1] dir_sum += each[1] ls_of_real_rects.append([(int(x_sum/len(c)), int(y_sum/len(c))), dir_sum/len(c)/pi*180]) return ls_of_real_rects

def find_in_neighbour(line, ls_of_cen_and_dir):

'''

Find all lines which locate in neighbourhood around line @param:

77

Khoa ĐT CLC – ĐHSPKT TP.HCM

line: a list contains center, direction, lenght of line format: float[center, direction, lenght]

ls_of_cen_and_dir: a list contains center, direction, lenght of all lines format: [[center0, direction0, lenght0], [center1, direction1, lenght1], ...] @return:

list of horizontal lines and vertical lines format: [[h1, h2, ...], [v1, v2, ...]] '''

center = line[0] direction = line[1] res = [[],[]]

for i, each in enumerate(ls_of_cen_and_dir): distance = get_distance_p2p(center, each[0])

if distance < rect_width*1.3 and distance >= rect_width*0.3: if fabs(direction-each[1]) < delta_dir:

res[0].append(i)

elif fabs(fabs(direction-each[1])-pi/2) < delta_dir: res[1].append(i)

elif distance < rect_width*0.3:

if fabs(direction-each[1]) < delta_dir: res[0].append(i)

return res

def get_center_direction_len(ls_of_lines):

'''

Calculate center, direction, lenght of all detected lines @param:

ls_of_lines: list of all lines defined by 2 end-points format: [(x1, y1, x2, y2), (x1, y1, x2, y2), ...] @return:

78

Khoa ĐT CLC – ĐHSPKT TP.HCM

format: [[center1, direction1, lenght1], [center2, direction2, lenght2], ...] '''

res = list()

for line in ls_of_lines: x1, y1, x2, y2 = line[0] p1 = (x1, y1) p2 = (x2, y2) line_center = [(x1+x2)//2, (y1+y2)//2] lenght = get_distance_p2p(p1, p2) if x1 == x2: line_direction = pi/2 elif y1 == y2: line_direction = 0 else: line_direction = atan((y1-y2)/(x1-x2)) res.append([line_center, line_direction, lenght])

return res (adsbygoogle = window.adsbygoogle || []).push({});

def find_clusters_of_paral_vert_lines(ls_of_lines):

'''

Find the neighbours of each line in ls_of_lines @param:

ls_of_lines: list of all lines defined by 2 end-points format: [(x1, y1, x2, y2), (x1, y1, x2, y2), ...] @return:

clusters_of_paral_vert_lines: list of all clusters format: [[[h1, h2, ...], [v1, v2, ...]], ...]

ls_of_cen_dir_len: a list contains center, direction, lenght of all lines format: float[[center, direction, lenght], ...]

'''

79

Khoa ĐT CLC – ĐHSPKT TP.HCM ls_of_cen_dir_len = get_center_direction_len(ls_of_lines)

for line in ls_of_cen_dir_len: if line[2] > rect_width*0.2:

# if lenght of line is longer than rect_width*0.2

clusters_of_paral_vert_lines.append(find_in_neighbour(line, ls_of_cen_dir_len)) return clusters_of_paral_vert_lines, ls_of_cen_dir_len

def auto_canny(image, sigma=0.33):

# compute the median of the single channel pixel intensities

v = np.median(image)

# apply automatic Canny edge detection using the computed median

lower = int(max(0, (1.0 - sigma) * v)) upper = int(min(255, (1.0 + sigma) * v)) edged = cv2.Canny(image, lower, upper)

# return the edged image

return edged

def detect_rects(img, color_center):

'''

@return:

ls_of_real_rects: a list contains center and directions of real rects format: [[(x0, y0), dir0], [(x1, y1), dir1], ...]

'''

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) blur = cv2.GaussianBlur(gray, (5, 5), 0)

edged = auto_canny(blur)

# edged = cv2.Canny(blur, 100, 200)

cv2.imshow("canny", edged)

80

Khoa ĐT CLC – ĐHSPKT TP.HCM ls_of_real_rects = [] (adsbygoogle = window.adsbygoogle || []).push({});

if lines is not None:

# print("num of lines:", len(lines))

clusters_of_paral_vert_lines, ls_of_cen_dir_len = find_clusters_of_paral_vert_lines(lines)

Một phần của tài liệu Ứng dụng dobot magician trong hệ thống phân loại sản phẩm (Trang 79)