日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > python >内容正文

python

python 椭圆检测_使用OpenCV和Python检测触摸/重叠圆/椭圆

發(fā)布時(shí)間:2023/12/14 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 椭圆检测_使用OpenCV和Python检测触摸/重叠圆/椭圆 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

這是我檢測(cè)圈子的嘗試.綜上所述

>執(zhí)行BGR-> HSV轉(zhuǎn)換,并使用V通道進(jìn)行處理

V通道:

>閾值,應(yīng)用形態(tài)關(guān)閉,然后進(jìn)行距離轉(zhuǎn)換(我會(huì)稱之為dist)

分區(qū)形象:

>創(chuàng)建一個(gè)模板.從圖像中的圓圈的大小,一個(gè)?75像素的半徑盤看起來很合理.把它的距離變換用作模板(我稱之為temp)

臨時(shí)形象:

>執(zhí)行模板匹配:dist * temp

dist * temp圖片:

>找到最終圖像的局部最大值.最大值的位置對(duì)應(yīng)于圓心,最大值對(duì)應(yīng)于它們的半徑

閾值模板匹配圖像:

檢測(cè)圈作為本地最大值:

我在C中做到這一點(diǎn),因?yàn)槲易钕矚g它.我覺得你可以很容易地把它轉(zhuǎn)換成python,如果你覺得這很有用.請(qǐng)注意,上述圖像不按比例.希望這可以幫助.

編輯:添加了Python版本

C :

double min, max;

Point maxLoc;

Mat im = imread("04Bxy.jpg");

Mat hsv;

Mat channels[3];

// bgr -> hsv

cvtColor(im, hsv, CV_BGR2HSV);

split(hsv, channels);

// use v channel for processing

Mat& ch = channels[2];

// apply Otsu thresholding

Mat bw;

threshold(ch, bw, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU);

// close small gaps

Mat kernel = getStructuringElement(MORPH_ELLIPSE, Size(3, 3));

Mat morph;

morphologyEx(bw, morph, CV_MOP_CLOSE, kernel);

// take distance transform

Mat dist;

distanceTransform(morph, dist, CV_DIST_L2, CV_DIST_MASK_PRECISE);

// add a black border to distance transformed image. we are going to do

// template matching. to get a good match for circles in the margin, we are adding a border

int borderSize = 75;

Mat distborder(dist.rows + 2*borderSize, dist.cols + 2*borderSize, dist.depth());

copyMakeBorder(dist, distborder,

borderSize, borderSize, borderSize, borderSize,

BORDER_CONSTANT | BORDER_ISOLATED, Scalar(0, 0, 0));

// create a template. from the sizes of the circles in the image,

// a ~75 radius disk looks reasonable, so the borderSize was selected as 75

Mat distTempl;

Mat kernel2 = getStructuringElement(MORPH_ELLIPSE, Size(2*borderSize+1, 2*borderSize+1));

// erode the ~75 radius disk a bit

erode(kernel2, kernel2, kernel, Point(-1, -1), 10);

// take its distance transform. this is the template

distanceTransform(kernel2, distTempl, CV_DIST_L2, CV_DIST_MASK_PRECISE);

// match template

Mat nxcor;

matchTemplate(distborder, distTempl, nxcor, CV_TM_CCOEFF_NORMED);

minMaxLoc(nxcor, &min, &max);

// threshold the resulting image. we should be able to get peak regions.

// we'll locate the peak of each of these peak regions

Mat peaks, peaks8u;

threshold(nxcor, peaks, max*.5, 255, CV_THRESH_BINARY);

convertScaleAbs(peaks, peaks8u);

// find connected components. we'll use each component as a mask for distance transformed image,

// then extract the peak location and its strength. strength corresponds to the radius of the circle

vector> contours;

vector hierarchy;

findContours(peaks8u, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));

for(int idx = 0; idx >= 0; idx = hierarchy[idx][0])

{

// prepare the mask

peaks8u.setTo(Scalar(0, 0, 0));

drawContours(peaks8u, contours, idx, Scalar(255, 255, 255), -1);

// find the max value and its location in distance transformed image using mask

minMaxLoc(dist, NULL, &max, NULL, &maxLoc, peaks8u);

// draw the circles

circle(im, maxLoc, (int)max, Scalar(0, 0, 255), 2);

}

蟒蛇:

import cv2

im = cv2.imread('04Bxy.jpg')

hsv = cv2.cvtColor(im, cv2.COLOR_BGR2HSV)

th, bw = cv2.threshold(hsv[:, :, 2], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))

morph = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)

dist = cv2.distanceTransform(morph, cv2.cv.CV_DIST_L2, cv2.cv.CV_DIST_MASK_PRECISE)

borderSize = 75

distborder = cv2.copyMakeBorder(dist, borderSize, borderSize, borderSize, borderSize,

cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0)

gap = 10

kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (2*(borderSize-gap)+1, 2*(borderSize-gap)+1))

kernel2 = cv2.copyMakeBorder(kernel2, gap, gap, gap, gap,

cv2.BORDER_CONSTANT | cv2.BORDER_ISOLATED, 0)

distTempl = cv2.distanceTransform(kernel2, cv2.cv.CV_DIST_L2, cv2.cv.CV_DIST_MASK_PRECISE)

nxcor = cv2.matchTemplate(distborder, distTempl, cv2.TM_CCOEFF_NORMED)

mn, mx, _, _ = cv2.minMaxLoc(nxcor)

th, peaks = cv2.threshold(nxcor, mx*0.5, 255, cv2.THRESH_BINARY)

peaks8u = cv2.convertScaleAbs(peaks)

contours, hierarchy = cv2.findContours(peaks8u, cv2.RETR_CCOMP, cv2.CHAIN_APPROX_SIMPLE)

peaks8u = cv2.convertScaleAbs(peaks) # to use as mask

for i in range(len(contours)):

x, y, w, h = cv2.boundingRect(contours[i])

_, mx, _, mxloc = cv2.minMaxLoc(dist[y:y+h, x:x+w], peaks8u[y:y+h, x:x+w])

cv2.circle(im, (int(mxloc[0]+x), int(mxloc[1]+y)), int(mx), (255, 0, 0), 2)

cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 255), 2)

cv2.drawContours(im, contours, i, (0, 0, 255), 2)

cv2.imshow('circles', im)

總結(jié)

以上是生活随笔為你收集整理的python 椭圆检测_使用OpenCV和Python检测触摸/重叠圆/椭圆的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。