日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析

瀏覽:42日期:2022-07-14 17:39:42

任務(wù)說(shuō)明:編寫一個(gè)錢幣定位系統(tǒng),其不僅能夠檢測(cè)出輸入圖像中各個(gè)錢幣的邊緣,同時(shí),還能給出各個(gè)錢幣的圓心坐標(biāo)與半徑。

效果

Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析

代碼實(shí)現(xiàn)

Canny邊緣檢測(cè):

# Author: Ji Qiu (BUPT)# filename: my_canny.pyimport cv2import numpy as npclass Canny: def __init__(self, Guassian_kernal_size, img, HT_high_threshold, HT_low_threshold): ’’’ :param Guassian_kernal_size: 高斯濾波器尺寸 :param img: 輸入的圖片,在算法過(guò)程中改變 :param HT_high_threshold: 滯后閾值法中的高閾值 :param HT_low_threshold: 滯后閾值法中的低閾值 ’’’ self.Guassian_kernal_size = Guassian_kernal_size self.img = img self.y, self.x = img.shape[0:2] self.angle = np.zeros([self.y, self.x]) self.img_origin = None self.x_kernal = np.array([[-1, 1]]) self.y_kernal = np.array([[-1], [1]]) self.HT_high_threshold = HT_high_threshold self.HT_low_threshold = HT_low_threshold def Get_gradient_img(self): ’’’ 計(jì)算梯度圖和梯度方向矩陣。 :return: 生成的梯度圖 ’’’ print (’Get_gradient_img’)new_img_x = np.zeros([self.y, self.x], dtype=np.float) new_img_y = np.zeros([self.y, self.x], dtype=np.float) for i in range(0, self.x): for j in range(0, self.y):if j == 0: new_img_y[j][i] = 1else: new_img_y[j][i] = np.sum(np.array([[self.img[j - 1][i]], [self.img[j][i]]]) * self.y_kernal)if i == 0: new_img_x[j][i] = 1else: new_img_x[j][i] = np.sum(np.array([self.img[j][i - 1], self.img[j][i]]) * self.x_kernal) gradient_img, self.angle = cv2.cartToPolar(new_img_x, new_img_y)#返回幅值和相位 self.angle = np.tan(self.angle) self.img = gradient_img.astype(np.uint8) return self.img def Non_maximum_suppression (self): ’’’ 對(duì)生成的梯度圖進(jìn)行非極大化抑制,將tan值的大小與正負(fù)結(jié)合,確定離散中梯度的方向。 :return: 生成的非極大化抑制結(jié)果圖 ’’’ print (’Non_maximum_suppression’)result = np.zeros([self.y, self.x]) for i in range(1, self.y - 1): for j in range(1, self.x - 1):if abs(self.img[i][j]) <= 4: result[i][j] = 0 continueelif abs(self.angle[i][j]) > 1: gradient2 = self.img[i - 1][j] gradient4 = self.img[i + 1][j] # g1 g2 # C # g4 g3 if self.angle[i][j] > 0: gradient1 = self.img[i - 1][j - 1] gradient3 = self.img[i + 1][j + 1] # g2 g1 # C # g3 g4 else: gradient1 = self.img[i - 1][j + 1] gradient3 = self.img[i + 1][j - 1]else: gradient2 = self.img[i][j - 1] gradient4 = self.img[i][j + 1] # g1 # g2 C g4 # g3 if self.angle[i][j] > 0: gradient1 = self.img[i - 1][j - 1] gradient3 = self.img[i + 1][j + 1] # g3 # g2 C g4 # g1 else: gradient3 = self.img[i - 1][j + 1] gradient1 = self.img[i + 1][j - 1]temp1 = abs(self.angle[i][j]) * gradient1 + (1 - abs(self.angle[i][j])) * gradient2temp2 = abs(self.angle[i][j]) * gradient3 + (1 - abs(self.angle[i][j])) * gradient4if self.img[i][j] >= temp1 and self.img[i][j] >= temp2: result[i][j] = self.img[i][j]else: result[i][j] = 0 self.img = result return self.img def Hysteresis_thresholding(self): ’’’ 對(duì)生成的非極大化抑制結(jié)果圖進(jìn)行滯后閾值法,用強(qiáng)邊延伸弱邊,這里的延伸方向?yàn)樘荻鹊拇怪狈较颍? 將比低閾值大比高閾值小的點(diǎn)置為高閾值大小,方向在離散點(diǎn)上的確定與非極大化抑制相似。 :return: 滯后閾值法結(jié)果圖 ’’’ print (’Hysteresis_thresholding’)for i in range(1, self.y - 1): for j in range(1, self.x - 1):if self.img[i][j] >= self.HT_high_threshold: if abs(self.angle[i][j]) < 1: if self.img_origin[i - 1][j] > self.HT_low_threshold: self.img[i - 1][j] = self.HT_high_threshold if self.img_origin[i + 1][j] > self.HT_low_threshold: self.img[i + 1][j] = self.HT_high_threshold # g1 g2 # C # g4 g3 if self.angle[i][j] < 0: if self.img_origin[i - 1][j - 1] > self.HT_low_threshold:self.img[i - 1][j - 1] = self.HT_high_threshold if self.img_origin[i + 1][j + 1] > self.HT_low_threshold:self.img[i + 1][j + 1] = self.HT_high_threshold # g2 g1 # C # g3 g4 else: if self.img_origin[i - 1][j + 1] > self.HT_low_threshold:self.img[i - 1][j + 1] = self.HT_high_threshold if self.img_origin[i + 1][j - 1] > self.HT_low_threshold:self.img[i + 1][j - 1] = self.HT_high_threshold else: if self.img_origin[i][j - 1] > self.HT_low_threshold: self.img[i][j - 1] = self.HT_high_threshold if self.img_origin[i][j + 1] > self.HT_low_threshold: self.img[i][j + 1] = self.HT_high_threshold # g1 # g2 C g4 # g3 if self.angle[i][j] < 0: if self.img_origin[i - 1][j - 1] > self.HT_low_threshold:self.img[i - 1][j - 1] = self.HT_high_threshold if self.img_origin[i + 1][j + 1] > self.HT_low_threshold:self.img[i + 1][j + 1] = self.HT_high_threshold # g3 # g2 C g4 # g1 else: if self.img_origin[i - 1][j + 1] > self.HT_low_threshold:self.img[i + 1][j - 1] = self.HT_high_threshold if self.img_origin[i + 1][j - 1] > self.HT_low_threshold:self.img[i + 1][j - 1] = self.HT_high_threshold return self.img def canny_algorithm(self): ’’’ 按照順序和步驟調(diào)用以上所有成員函數(shù)。 :return: Canny 算法的結(jié)果 ’’’ self.img = cv2.GaussianBlur(self.img, (self.Guassian_kernal_size, self.Guassian_kernal_size), 0) self.Get_gradient_img() self.img_origin = self.img.copy() self.Non_maximum_suppression() self.Hysteresis_thresholding() return self.img

Hough變換

# Author: Ji Qiu (BUPT)# filename: my_hough.pyimport numpy as npimport mathclass Hough_transform: def __init__(self, img, angle, step=5, threshold=135): ’’’ :param img: 輸入的圖像 :param angle: 輸入的梯度方向矩陣 :param step: Hough 變換步長(zhǎng)大小 :param threshold: 篩選單元的閾值 ’’’ self.img = img self.angle = angle self.y, self.x = img.shape[0:2] self.radius = math.ceil(math.sqrt(self.y**2 + self.x**2)) self.step = step self.vote_matrix = np.zeros([math.ceil(self.y / self.step), math.ceil(self.x / self.step), math.ceil(self.radius / self.step)]) self.threshold = threshold self.circles = [] def Hough_transform_algorithm(self): ’’’ 按照 x,y,radius 建立三維空間,根據(jù)圖片中邊上的點(diǎn)沿梯度方向?qū)臻g中的所有單 元進(jìn)行投票。每個(gè)點(diǎn)投出來(lái)結(jié)果為一折線。 :return: 投票矩陣 ’’’ print (’Hough_transform_algorithm’)for i in range(1, self.y - 1): for j in range(1, self.x - 1):if self.img[i][j] > 0: y = i x = j r = 0 while y < self.y and x < self.x and y >= 0 and x >= 0: self.vote_matrix[math.floor(y / self.step)][math.floor(x / self.step)][math.floor(r / self.step)] += 1 y = y + self.step * self.angle[i][j] x = x + self.step r = r + math.sqrt((self.step * self.angle[i][j])**2 + self.step**2) y = i - self.step * self.angle[i][j] x = j - self.step r = math.sqrt((self.step * self.angle[i][j])**2 + self.step**2) while y < self.y and x < self.x and y >= 0 and x >= 0: self.vote_matrix[math.floor(y / self.step)][math.floor(x / self.step)][math.floor(r / self.step)] += 1 y = y - self.step * self.angle[i][j] x = x - self.step r = r + math.sqrt((self.step * self.angle[i][j])**2 + self.step**2) return self.vote_matrix def Select_Circle(self): ’’’ 按照閾值從投票矩陣中篩選出合適的圓,并作極大化抑制,這里的非極大化抑制我采 用的是鄰近點(diǎn)結(jié)果取平均值的方法,而非單純的取極大值。 :return: None ’’’ print (’Select_Circle’)houxuanyuan = [] for i in range(0, math.ceil(self.y / self.step)): for j in range(0, math.ceil(self.x / self.step)):for r in range(0, math.ceil(self.radius / self.step)): if self.vote_matrix[i][j][r] >= self.threshold: y = i * self.step + self.step / 2 x = j * self.step + self.step / 2 r = r * self.step + self.step / 2 houxuanyuan.append((math.ceil(x), math.ceil(y), math.ceil(r))) if len(houxuanyuan) == 0: print('No Circle in this threshold.') return x, y, r = houxuanyuan[0] possible = [] middle = [] for circle in houxuanyuan: if abs(x - circle[0]) <= 20 and abs(y - circle[1]) <= 20:possible.append([circle[0], circle[1], circle[2]]) else:result = np.array(possible).mean(axis=0)middle.append((result[0], result[1], result[2]))possible.clear()x, y, r = circlepossible.append([x, y, r]) result = np.array(possible).mean(axis=0) middle.append((result[0], result[1], result[2])) def takeFirst(elem): return elem[0] middle.sort(key=takeFirst) x, y, r = middle[0] possible = [] for circle in middle: if abs(x - circle[0]) <= 20 and abs(y - circle[1]) <= 20:possible.append([circle[0], circle[1], circle[2]]) else:result = np.array(possible).mean(axis=0)print('Circle core: (%f, %f) Radius: %f' % (result[0], result[1], result[2]))self.circles.append((result[0], result[1], result[2]))possible.clear()x, y, r = circlepossible.append([x, y, r]) result = np.array(possible).mean(axis=0) print('Circle core: (%f, %f) Radius: %f' % (result[0], result[1], result[2])) self.circles.append((result[0], result[1], result[2])) def Calculate(self): ’’’ 按照算法順序調(diào)用以上成員函數(shù) :return: 圓形擬合結(jié)果圖,圓的坐標(biāo)及半徑集合 ’’’ self.Hough_transform_algorithm() self.Select_Circle() return self.circles

調(diào)用

# Author: Ji Qiu (BUPT)# filename: main.pyimport cv2import mathfrom my_hough import Hough_transformfrom my_canny import Canny# np.set_printoptions(threshold=np.inf)Path = 'picture_source/picture.jpg'Save_Path = 'picture_result/'Reduced_ratio = 2Guassian_kernal_size = 3HT_high_threshold = 25HT_low_threshold = 6Hough_transform_step = 6Hough_transform_threshold = 110if __name__ == ’__main__’: img_gray = cv2.imread(Path, cv2.IMREAD_GRAYSCALE) img_RGB = cv2.imread(Path) y, x = img_gray.shape[0:2] img_gray = cv2.resize(img_gray, (int(x / Reduced_ratio), int(y / Reduced_ratio))) img_RGB = cv2.resize(img_RGB, (int(x / Reduced_ratio), int(y / Reduced_ratio))) # canny takes about 40 seconds print (’Canny ...’) canny = Canny(Guassian_kernal_size, img_gray, HT_high_threshold, HT_low_threshold) canny.canny_algorithm() cv2.imwrite(Save_Path + 'canny_result.jpg', canny.img) # hough takes about 30 seconds print (’Hough ...’) Hough = Hough_transform(canny.img, canny.angle, Hough_transform_step, Hough_transform_threshold) circles = Hough.Calculate() for circle in circles: cv2.circle(img_RGB, (math.ceil(circle[0]), math.ceil(circle[1])), math.ceil(circle[2]), (28, 36, 237), 2) cv2.imwrite(Save_Path + 'hough_result.jpg', img_RGB) print (’Finished!’)

運(yùn)行效果

Python實(shí)現(xiàn)Canny及Hough算法代碼實(shí)例解析

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
日本不卡不码高清免费观看,久久国产精品久久w女人spa,黄色aa久久,三上悠亚国产精品一区二区三区
午夜久久av| 99精品视频在线| 欧美日韩国产高清电影| 精品国产第一福利网站| 91麻豆国产自产在线观看亚洲| 麻豆91在线播放| 欧美精品二区| 国产日韩一区| 91国内精品| 国产欧美激情| 久久99偷拍| 国产一区2区在线观看| 精品久久精品| 蜜桃成人精品| 久久激情一区| 激情综合亚洲| 每日更新成人在线视频| 亚洲我射av| 国产日韩欧美中文在线| 麻豆久久一区二区| 黄色精品视频| 四虎4545www国产精品| 久久久久亚洲| 黄色免费成人| 涩涩涩久久久成人精品| 欧美中文一区| 韩国女主播一区二区三区| 欧美片第1页| 亚洲一区区二区| 日韩国产欧美在线播放| 欧美精品成人| 性欧美videohd高精| 午夜日韩福利| 日本一区免费网站| 久久av中文| 偷拍精品精品一区二区三区| 91精品1区| 蜜桃av一区二区| 国产日韩一区二区三区在线播放| 国产一区二区三区四区大秀| 欧美肉体xxxx裸体137大胆| 日韩在线播放一区二区| 91麻豆精品激情在线观看最新| 国产精品777777在线播放| 极品av在线| 日韩视频二区| 欧美一区在线观看视频| 日韩成人免费| 久热re这里精品视频在线6| 国产精品一区二区三区www| 欧美黄色网页| 中文字幕日韩欧美精品高清在线| 奇米色欧美一区二区三区| 国产一区二区三区亚洲| 午夜国产精品视频| 久久99久久人婷婷精品综合| 亚洲高清不卡| 欧美午夜三级| 欧美 日韩 国产精品免费观看| 一区二区三区午夜视频| 国产一区二区三区四区二区| 在线综合亚洲| 精品国产不卡| 综合一区二区三区| 亚洲人成在线网站| 日韩精品国产欧美| 韩国精品主播一区二区在线观看 | 国产精品亚洲综合久久| 色吊丝一区二区| 日韩欧美中文在线观看| 欧美日韩尤物久久| 国产毛片一区二区三区| 狠狠干成人综合网| 国产一区国产二区国产三区| 在线免费观看亚洲| 色吊丝一区二区| 国产精品久久久久av蜜臀| 婷婷激情久久| 国产精品v亚洲精品v日韩精品| 欧美日韩国产精品一区二区亚洲| 国产日韩欧美三级| 国产精品美女| 电影天堂国产精品| 国产亚洲精品美女久久| 99xxxx成人网| 欧美日韩尤物久久| 九九久久国产| 国产精品一二| 亚洲欧洲免费| 欧美日韩激情| 久久精品电影| 成人影视亚洲图片在线| 国产精品一区毛片| 日韩中文字幕一区二区高清99| 欧美精品一二| 久久精品国产一区二区| 亚洲91在线| 99视频精品免费观看| sm捆绑调教国产免费网站在线观看| 欧美一级一区| 亚洲一区二区三区久久久| 久久九九99| 国产中文在线播放| 欧美aa在线视频| 亚洲18在线| 亚洲精品在线观看91| 久久久久亚洲| 亚洲爱爱视频| 日本不良网站在线观看| 精品欠久久久中文字幕加勒比| 午夜精品影视国产一区在线麻豆| 性一交一乱一区二区洋洋av| 在线看片不卡| 欧美天堂亚洲电影院在线观看| 久久精品在线| 久久精品动漫| 久久久国产亚洲精品| 亚洲一级少妇| 久久久国产精品一区二区中文| 伊人久久国产| 日韩精品久久久久久久电影99爱| 精品国产免费人成网站| 精品视频在线一区二区在线| 欧美黄色一区| 麻豆久久久久久久| 国产精品一在线观看| 日本午夜精品一区二区三区电影| 日韩一二三区在线观看| 四虎精品一区二区免费| 免费成人在线视频观看| 亚洲伊人精品酒店| 日本一区二区中文字幕| 欧美日韩精品一区二区三区在线观看| 18国产精品| 久久久久久久久成人| 国产美女精品视频免费播放软件| 国产乱码精品一区二区三区四区 | 欧美xxxx中国| 最近高清中文在线字幕在线观看1| 高清日韩中文字幕| 国产不卡人人| 日本欧美不卡| 欧美日韩激情在线一区二区三区| 99国产精品| 亚州欧美在线| 国产精品亚洲二区| 加勒比视频一区| 国产超碰精品| 中文精品在线| 日本在线观看不卡视频| 国产欧美一区二区三区精品观看| 精品高清久久| 美女网站视频一区| 国产精品婷婷| 日本欧美在线| 国产一区二区三区四区大秀| 日韩一区二区中文| 尤物网精品视频| 日本亚洲三级在线| 麻豆视频一区| 久久婷婷亚洲| 美女日韩在线中文字幕| 青青草伊人久久| 最新中文字幕在线播放 | 麻豆精品蜜桃| 国产色综合网| 亚洲精品伊人| 精品国产乱码久久久| 国产91久久精品一区二区| 日韩一区欧美二区| 国产精品宾馆| 欧美精选一区二区三区| 亚洲ww精品| 国产精品久久久久蜜臀| aa亚洲婷婷| 国产精品任我爽爆在线播放| 国产日韩电影| 亚洲一区欧美| 91亚洲国产| 中文无码日韩欧| 精品一区不卡| 欧美+亚洲+精品+三区| 亚州欧美在线| 五月天av在线| 日韩精品三级| 久久久精品日韩| 日韩va亚洲va欧美va久久| 日本不良网站在线观看| 综合欧美精品| 麻豆国产在线| 深夜日韩欧美| 日韩国产激情| 日本va欧美va瓶| 久久久久久自在自线| 一区二区三区网站| 伊人久久在线| 亚洲精品一二| 成人羞羞在线观看网站| 国产亚洲精品美女久久| 婷婷综合五月|