いくつかのOPENCV常用アルゴリズム(python版とC++版)
pythonバージョン個人でよく使われる
以前使っていたC++
1、よく使うアルゴリズムは境界を探す
2、点の中で最小包囲円を探す
3.点を含む最小長方形
4.四辺に平行な正の長方形
5.カスタムボリューム
6、膨張と腐食
7、反転
import cv2
import numpy as np
def show_img(name="", img="", x=0, y=0):
cv2.namedWindow(name, 0) #
cv2.moveWindow(name, x, y) # ,
cv2.imshow(name, img) #
img_src = cv2.imread("1.jpg", 1) #
cv2.medianBlur(img_src, 5) #
show_img(name="src_img", img=img_src, x=0, y=0)
img_src = cv2.resize(img_src, (400, 400), cv2.INTER_AREA) #
h_flip = cv2.flip(img_src, 1) # 1: 0: -1:0&1
show_img(name="flip_img", img=h_flip, x=0, y=400)
img_black = np.zeros(img_src.shape, np.uint8) # np.uint8
img_src = cv2.cvtColor(img_src, cv2.COLOR_BGR2GRAY) #
_, img_bin = cv2.threshold(img_src, 200, 255, cv2.THRESH_BINARY) #
show_img(name="bin_img", img=img_bin, x=800, y=0)
kernel = np.ones((5, 5), np.uint8) #
img_erode = cv2.erode(img_bin, kernel) # cv2.dilate
# opening = cv2.morphologyEx(img, cv2.MORPH_OPEN, kernel) # cv2.MORPH_CLOSE
show_img(name="erode_img", img=img_erode, x=1200, y=0)
cnt_, _ = cv2.findContours(img_bin, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #
cv2.drawContours(img_black, cnt_, -1, (255, 255, 255), 1)
x, y, w, h = cv2.boundingRect(img_bin) # , 255
cv2.line(img_black,(x,y),(x + w, y + h),(0,255,0), 3)
cv2.rectangle(img_black, (x, y), (x + w, y + h), (255, 0, 255), 1) # cv2.polylines, cv2.ellipse
cv2.circle(img_black, (int(x+w/2),int(y+h/2)), 30, (0,0,213),-1)
font=cv2.FONT_HERSHEY_SIMPLEX
cv2.putText(img_black,'OpenCV',(80,90), font, 2,(255,255,255),3) #
for i in range(len(cnt_)): #
x, y, w, h = cv2.boundingRect(cnt_[i]) #
cv2.rectangle(img_black, (x, y), (x + w, y + h), (0, 0, 255), 1)
show_img(name="black_img", img=img_black, x=400, y=0)
img_med = img_black[50: img_black.shape[0] - 50, 50: img_black.shape[1] - 50] #
show_img(name="med_img", img=img_med, x=400, y=400)
cv2.waitKey(5000)
# b = a.astype(np.uint8) #
以前使っていたC++
1、よく使うアルゴリズムは境界を探す
std::vector<:vector>> contours;
findContours( imgLast, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cv::Mat imgMask( matBrokeRoi.size(), CV_8UC1, cv::Scalar(0) );
for ( unsigned int i=0; i < contours.size(); i++ )
{
double targetArea = contourArea(contours[i]);
cv::Rect rect = cv::boundingRect(contours[i]);
drawContours(imgMask, contours, (int)i, cv::Scalar::all(255), -1, 8);
}
2、点の中で最小包囲円を探す
Point2f center;
float radius;
minEnclosingCircle(points, center, radius);
3.点を含む最小長方形
rect = minAreaRect(InputArray points)
4.四辺に平行な正の長方形
Rect boundingRect(InputArray points)
5.カスタムボリューム
#include
int main ()
{
cv::Mat imgSrc,imgGrad;
imgSrc=cv::imread("12.jpg");
cv::Mat fkernel = cv::Mat(2, 1, CV_32FC1, cv::Scalar(0) );
fkernel.at(0,0) = -1;
fkernel.at(1,0) = 1;
//Filter_kernel_Vert.at(2,0) = 1;
cv::resize( imgSrc, imgSrc, cv::Size( imgSrc.cols / 2, imgSrc.rows / 2 ) );
cv::filter2D( imgSrc, imgGrad, imgSrc.depth(), fkernel );
cv::imshow("temp2",imgGrad);
cv::waitKey(0);
return 0;
}
6、膨張と腐食
cv::Mat KerForCannyEro = cv::Mat(3, 9, CV_8UC1, cv::Scalar(1) );
cv::dilate( matCanny, matCannyDilate, KerForCannyEro );
cv::erode( matCanny, matCannyDilate, KerForCannyEro )
7、反転
cv::flip(imgSrc,imgOut,1);