以下是一些OpenCV中常用的代码片段,涵盖了一些基本的图像处理和计算机视觉任务。
-
import cv2 # 读取图像 img = cv2.imread('image.jpg') # 显示图像 cv2.imshow('Image', img) cv2.waitKey(0) cv2.destroyAllWindows()
-
resized_img = cv2.resize(img, (width, height))
-
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
-
smoothed_img = cv2.GaussianBlur(img, (kernel_size, kernel_size), 0)
-
edges = cv2.Canny(gray_img, threshold1, threshold2)
-
ret, binary_img = cv2.threshold(gray_img, threshold_value, max_value, cv2.THRESH_BINARY)
-
contours, hierarchy = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
-
cv2.drawContours(img, contours, -1, (0, 255, 0), 2)
-
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') faces = face_cascade.detectMultiScale(gray_img, scaleFactor=1.3, minNeighbors=5)
-
图像旋转:
rows, cols = img.shape[:2] M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, scale) rotated_img = cv2.warpAffine(img, M, (cols, rows))
-
图像腐蚀与膨胀:
kernel = np.ones((kernel_size, kernel_size), np.uint8) eroded_img = cv2.erode(img, kernel, iterations=1) dilated_img = cv2.dilate(img, kernel, iterations=1)
-
equ_img = cv2.equalizeHist(gray_img)
-
图像混合:
blended_img = cv2.addWeighted(img1, alpha, img2, beta, gamma)
-
图像拼接:
stitched_img = cv2.hconcat([img1, img2])
-
图像截取:
roi = img[y:y+h, x:x+w]
-
图像相加:
added_img = cv2.add(img1, img2)
-
图像减法:
subtracted_img = cv2.subtract(img1, img2)
-
图像位运算:
bitwise_and = cv2.bitwise_and(img1, img2) bitwise_or = cv2.bitwise_or(img1, img2) bitwise_xor = cv2.bitwise_xor(img1, img2) bitwise_not = cv2.bitwise_not(img)
-
图像平均模糊:
averaged_img = cv2.blur(img, (kernel_size, kernel_size))
-
中值滤波:
median_blurred_img = cv2.medianBlur(img, ksize)
-
adaptive_thresh = cv2.adaptiveThreshold( gray_img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
-
M = cv2.getRotationMatrix2D((cols / 2, rows / 2), angle, scale) rotated_resized_img = cv2.warpAffine(img, M, (new_cols, new_rows))
-
sift = cv2.SIFT_create() keypoints1, descriptors1 = sift.detectAndCompute(img1, None) keypoints2, descriptors2 = sift.detectAndCompute(img2, None)
-
surf = cv2.SURF_create() keypoints1, descriptors1 = surf.detectAndCompute(img1, None) keypoints2, descriptors2 = surf.detectAndCompute(img2, None)
-
orb = cv2.ORB_create() keypoints1, descriptors1 = orb.detectAndCompute(img1, None) keypoints2, descriptors2 = orb.detectAndCompute(img2, None)
-
FLANN_INDEX_KDTREE = 1 index_params = dict(algorithm=FLANN_INDEX_KDTREE, trees=5) search_params = dict(checks=50) flann = cv2.FlannBasedMatcher(index_params, search_params) matches = flann.knnMatch(descriptors1, descriptors2, k=2)
-
net = cv2.dnn.readNet('model.weights', 'model.cfg') blob = cv2.dnn.blobFromImage(img, scalefactor=1.0, size=(300, 300), mean=(104.0, 177.0, 123.0)) net.setInput(blob) detections = net.forward()
-
hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) boxes, weights = hog.detectMultiScale(img, winStride=(8, 8), padding=(0, 0), scale=1.05)
这些是一些基本的OpenCV代码示例,覆盖了图像处理和计算机视觉中的常见任务。在实际应用中,可能需要根据具体场景和需求进一步调整参数和算法。
原文地址:https://blog.csdn.net/weixin_42367888/article/details/134528881
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_8907.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。