几个OPENCV常用算法(python版及C++版)

 python版本个人常用的

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> 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);

 

你可能感兴趣的:(C++,opencv,笔记)