当前位置: 首页 > news >正文

做网站要不要营业执照网站规划对网站建设起到

做网站要不要营业执照,网站规划对网站建设起到,公司网站建设开题报告,东营今天的消息文章目录 前言一、几何变换1.1 缩放1.2 平移1.3 旋转1.4 翻转1.5 仿射1.6 透视 二、低通滤波2.1 均值滤波2.2 高斯滤波2.3 中值滤波2.4 双边滤波2.5 自定义滤波 三、高通滤波3.1 Sobel3.2 Scharr3.3 Laplacian3.4 Canny 四、图像金字塔4.1 高斯金字塔4.2 拉普拉斯金字塔 五、形… 文章目录 前言一、几何变换1.1 缩放1.2 平移1.3 旋转1.4 翻转1.5 仿射1.6 透视 二、低通滤波2.1 均值滤波2.2 高斯滤波2.3 中值滤波2.4 双边滤波2.5 自定义滤波 三、高通滤波3.1 Sobel3.2 Scharr3.3 Laplacian3.4 Canny 四、图像金字塔4.1 高斯金字塔4.2 拉普拉斯金字塔 五、形态学5.1 腐蚀5.2 膨胀5.3 运算 六、直方图6.1 计算6.2 均衡6.3 反向投影 七、轮廓7.1 查找显示7.2 常用特征 参考 前言 图像处理是计算机视觉领域中的核心技术之一它涉及到对图像进行各种变换、滤波、金字塔构建、形态学操作等一系列处理。在本篇博文中我们将深入探讨使用OpenCV和Python进行图像处理的各种技术和方法。从几何变换到滤波、金字塔构建再到形态学操作我们将逐步介绍并实践这些重要的图像处理技术帮助读者更好地理解和应用于实际项目中。 一、几何变换 1.1 缩放 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef generateCanvas(img, img2):canvas np.zeros((max(img.shape[0], img2.shape[0]),img.shape[1] img2.shape[1], 3), dtypenp.uint8)# 将图像1和图像2放置在画布上canvas[:img.shape[0], :img.shape[1]] imgcanvas[:img2.shape[0],img.shape[1]:] img2return canvasdef main():img cv.imread(sudoku.png)assert img is not None, file could not be read, check with os.path.exists()# dsize输出图像的大小。如果这个参数不为None那么就代表将原图像缩放到这个Size(widthheight)指定的大小# 如果这个参数为0那么原图像缩放之后的大小就要通过下面的公式来计算#               dsize Size(round(fx*src.cols), round(fy*src.rows))# 其中fx和fy就是下面要说的两个参数是图像width方向和height方向的缩放比例。#  fxwidth方向的缩放比例如果它是0那么它就会按照(double)dsize.width/src.cols来计算#  fyheight方向的缩放比例如果它是0那么它就会按照(double)dsize.height/src.rows来计算# interpolation这个是指定插值的方式图像缩放之后肯定像素要进行重新计算的就靠这个参数来指定重新计算像素的方式有以下几种# INTER_NEAREST - 最近邻插值# INTER_LINEAR - 双线性插值如果最后一个参数你不指定默认使用这种方法# INTER_AREA - 使用像素区域关系进行重采样# INTER_CUBIC - 4x4像素邻域内 的双立方插值# INTER_LANCZOS4 - 8x8像素邻域内的Lanczos插值fx 0.6fy 0.6img_half_nearest cv.resize(img, dsizeNone, fxfx, fyfy, interpolationcv.INTER_NEAREST)img_half_linear cv.resize(img, dsizeNone, fxfx, fyfy, interpolationcv.INTER_LINEAR)img_half_area cv.resize(img, dsizeNone, fxfx,fyfy, interpolationcv.INTER_AREA)img_half_cubic cv.resize(img, dsizeNone, fxfx, fyfy, interpolationcv.INTER_CUBIC)img_half_lanczos4 cv.resize(img, dsizeNone, fxfx, fyfy, interpolationcv.INTER_LANCZOS4)titles [nearest, linear, area, cubic, Lanczos]images [img_half_nearest, img_half_linear,img_half_area, img_half_cubic, img_half_lanczos4]# canvas generateCanvas(img, img_half_linear)# cv.imshow(test, canvas)for i in range(len(images)):show cv.cvtColor(generateCanvas(img, images[i]), cv.COLOR_BGR2RGB)plt.subplot(2, 3, i1), plt.imshow(show)plt.title(titles[i])plt.xticks([]), plt.yticks([])plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()1.2 平移 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()rows, cols, _ img.shapeM np.float32([[1, 0, 100], [0, 1, 50]])# M: 2 * 3 矩阵# 根据下面的公式想实现平移可以通过构造M[[1, 0, x], [0, 1, y]]实现向右平移x向下平移y# dst(x, y) (M[0,0] * x M[0, 1] * y M[0, 2], M[1, 0] * x M[1, 1] * y M[1, 2])img_right_down cv.warpAffine(img, M, (cols, rows))M np.float32([[1, 0, -100], [0, 1, -50]])img_left_up cv.warpAffine(img, M, (cols, rows))titles [origin, right down, left up]images [img, img_right_down, img_left_up]for i in range(len(images)):show cv.cvtColor(images[i], cv.COLOR_BGR2RGB)plt.subplot(1, 3, i1), plt.imshow(show)plt.title(titles[i])plt.xticks([]), plt.yticks([])plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()1.3 旋转 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()rows, cols, _ img.shapeM cv.getRotationMatrix2D(center(rows / 2, cols / 2), angle-30, scale1)img_rotete_minus_30 cv.warpAffine(img, M, (cols, rows))M cv.getRotationMatrix2D(center(rows / 2, cols / 2), angle30, scale1)img_rotete_30 cv.warpAffine(img, M, (cols, rows))titles [origin, rotate -30 degree, rotate 30 degree]images [img, img_rotete_minus_30, img_rotete_30]for i in range(len(images)):show cv.cvtColor(images[i], cv.COLOR_BGR2RGB)plt.subplot(1, 3, i1), plt.imshow(show)plt.title(titles[i])plt.xticks([]), plt.yticks([])plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()1.4 翻转 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()# 竖直翻转img_vertical cv.flip(img, flipCode1)# 水平翻转img_horizontal cv.flip(img, flipCode0)# 两者img_both cv.flip(img, flipCode-1)title [Origin, flipCode1,Vertical,flipCode0,Horizontal, flipCode-1,Both]# 对应的图像imgs [img, img_vertical, img_horizontal, img_both]for i in range(len(imgs)):plt.subplot(2, 2, i 1)plt.imshow(cv.cvtColor(imgs[i], cv.COLOR_BGR2RGB))plt.title(title[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()1.5 仿射 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()rows, cols, _ img.shape# pts1 表示变换前三个点位置# pts2 表示变换后三个点位置pts1 np.float32([[270, 270], [330, 270], [310, 320]])pts2 np.float32([[100, 100], [150, 50], [150, 100]])M cv.getAffineTransform(pts1, pts2)img_result cv.warpAffine(img, M, (cols, rows))for p in pts1:cv.circle(img, center(int(p[0]), int(p[1])), radius3,color(0, 0, 255), thicknesscv.FILLED)for p in pts2:cv.circle(img_result, center(int(p[0]), int(p[1])), radius3,color(0, 255, 0), thicknesscv.FILLED)title [Origin, Affine]# 对应的图像imgs [img, img_result]for i in range(len(imgs)):plt.subplot(1, len(imgs), i 1)plt.imshow(cv.cvtColor(imgs[i], cv.COLOR_BGR2RGB))plt.title(title[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()1.6 透视 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()rows, cols, _ img.shape# 将图像投影到一个新的视平面需要四个点, 在这4个点中有3个点不应共线# 平面的4个点顺序为 左上 右上 左下 右下pts1 np.float32([[270, 270], [330, 270], [270, 350], [320, 350]])pts2 np.float32([[0, 0], [512, 0], [0, 512], [512, 512]])M cv.getPerspectiveTransform(pts1, pts2)img_result cv.warpPerspective(img, M, (cols, rows))for p in pts1:cv.circle(img, center(int(p[0]), int(p[1])), radius3,color(0, 0, 255), thicknesscv.FILLED)for p in pts2:cv.circle(img_result, center(int(p[0]), int(p[1])), radius3,color(0, 255, 0), thicknesscv.FILLED)title [Origin, Perspective,]# 对应的图像imgs [img, img_result]for i in range(len(imgs)):plt.subplot(1, len(imgs), i 1)plt.imshow(cv.cvtColor(imgs[i], cv.COLOR_BGR2RGB))plt.title(title[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()二、低通滤波 与一维信号一样图像也可以用各种低通滤波器low-pass filtersLPF、高通滤波器high-pass filtersHPF等进行过滤 LPF 用于降低某些像素强度可以用于平滑图像保留图像中的低频成分过滤高频成分帮助去除噪点模糊图像等HPF 用于增强某些像素强度可以用于帮助寻找图像中的边缘 2.1 均值滤波 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lenaNoise.png)assert img is not None, file could not be read, check with os.path.exists()# 均值滤波: 简单的平均卷积操作result cv.blur(img, ksize(5, 5))# 显示图像titles [origin image, blur image]images [img, result]for i in range(2):plt.subplot(1, 2, i1), plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()2.2 高斯滤波 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lenaNoise.png)assert img is not None, file could not be read, check with os.path.exists()# 高斯滤波:# ksize: 高斯核大小可以是0或正奇数当为0从sigma计算result_0 cv.GaussianBlur(img, ksize(5, 5), sigmaX0)# 显示图像titles [origin image, gaussian blur]images [img, result_0]for i in range(len(images)):plt.subplot(1, len(images), i1), plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()2.3 中值滤波 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lenaNoise.png)assert img is not None, file could not be read, check with os.path.exists()# 中值滤波:result_0 cv.medianBlur(img, ksize3)result_1 cv.medianBlur(img, ksize5)result_2 cv.medianBlur(img, ksize7)# 显示图像titles [origin image, ksize3, ksize5, ksize7]images [img, result_0, result_1, result_2]for i in range(len(images)):plt.subplot(2, 2, i1), plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main() 2.4 双边滤波 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lenaNoise.png)assert img is not None, file could not be read, check with os.path.exists()# 双边滤波: 在计算像素值的同时会考虑距离和色差信息从而可在消除噪声得同时保护边缘信息可用于美颜# d 每个像素邻域的直径当0时从sigmaSpace计算 注意当 d 5 非常慢所以建议使用5作为实时应用或者使用9作为需要重噪声过滤的离线应用# sigmaColor 颜色标准方差一般尽可能大较大的值表示在颜色相近的区域内像素将会被更多地保留# sigmaSpace 坐标空间标准方差(像素单位)一般尽可能小较小的值表示在距离中心像素较远的像素的权重较小result_0 cv.bilateralFilter(img, d0, sigmaColor100, sigmaSpace15)result_1 cv.bilateralFilter(img, d0, sigmaColor50, sigmaSpace15)# 显示图像titles [origin image, color100 space5, color50 space5]images [img, result_0, result_1]for i in range(len(images)):plt.subplot(1, len(images), i1), plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()2.5 自定义滤波 import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef main():img cv.imread(lenaNoise.png)assert img is not None, file could not be read, check with os.path.exists()# 自定义滤波,对图像进行卷积运算:# 先定义卷积核再参与计算ddepth-1表示和源图像深度一致# 此外还有个参数anchor默认值为(-1,-1)表示锚点位于内核中心kernel np.ones((5, 5), np.float32)/25result cv.filter2D(img, ddepth-1, kernelkernel)# 显示图像titles [origin image, filter2d filter,]images [img, result]for i in range(len(images)):plt.subplot(1, len(images), i1), plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()三、高通滤波 3.1 Sobel import cv2 as cv import numpy as np from matplotlib import pyplot as pltdef sobel(img):# Sobel 使用Sobel算子计算一阶、二阶、三阶或三者混合 图像导数梯度# ddepth 输出图像的深度计算图像的梯度会有浮点数负数所以后面会取绝对值# dxdy X和Y方向的梯度# ksize 参与图像卷积操作的核大小可以是1、3、5或7用于不同精度的边缘检测grad_x cv.Sobel(img, ddepthcv.CV_64F, dx1, dy0, ksize3)abs_grad_x cv.convertScaleAbs(grad_x)grad_y cv.Sobel(img, ddepthcv.CV_64F, dx0, dy1, ksize3)abs_grad_y cv.convertScaleAbs(grad_y)# 分别计算x和y再求和sobel cv.addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0)return abs_grad_x, abs_grad_y, sobeldef main():img cv.imread(res2.jpg)img_lena cv.imread(lena.png, cv.IMREAD_GRAYSCALE)assert img is not None and img_lena is not None, file could not be read, check with os.path.exists()# 显示图像titles [origin , sobel_x, sobel_y, sobel_xsobel_y, lena ,lena_sobel_x, lena_sobel_y, lena_sobel_xlena_sobel_y]images [img, *sobel(img), img_lena, *sobel(img_lena)]for i in range(len(images)):plt.subplot(2, 4, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()3.2 Scharr import cv2 as cv import numpy as np from matplotlib import pyplot as plt plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png, cv.IMREAD_GRAYSCALE)assert img is not None, file could not be read, check with os.path.exists()scharrx cv.Scharr(img, ddepthcv.CV_64F, dx1, dy0)scharrxAbs cv.convertScaleAbs(scharrx)scharry cv.Scharr(img, ddepthcv.CV_64F, dx0, dy1)scharryAbs cv.convertScaleAbs(scharry)# 分别计算x和y再求和scharrxy cv.addWeighted(scharrxAbs, 0.5, scharryAbs, 0.5, 0)# 显示图像titles [origin , scharrx, scharry, scharrx scharry]images [img, scharrxAbs, scharryAbs, scharrxy]for i in range(len(images)):plt.subplot(2, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()3.3 Laplacian import cv2 as cv import numpy as np from matplotlib import pyplot as plt plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png, cv.IMREAD_GRAYSCALE)assert img is not None, file could not be read, check with os.path.exists()result cv.Laplacian(img, cv.CV_16S, ksize3)resultAbs cv.convertScaleAbs(result)# 显示图像titles [origin , Laplacian]images [img, resultAbs]for i in range(len(images)):plt.subplot(1, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()3.4 Canny import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()# canny 是一个多步算法先用5*5核高斯滤波过滤噪声然后用Sobel算法查找图像梯度最后去除任何可能不构成边缘的、不需要的像素# threshold1 较小值 低于这个值的肯定不是边# threshold2 较大值 高于这个值的肯定是边# 位于两者之间的需要进行连通性判断img_gray cv.cvtColor(img, cv.COLOR_BGR2GRAY)img_result_1 cv.Canny(img_gray, threshold1100, threshold2200)img_result_2 cv.Canny(img_gray, threshold150, threshold2240)# 显示图像titles [origin , gray, canny 100,200, canny 50,240]images [img, img_gray, img_result_1, img_result_2]for i in range(len(images)):plt.subplot(2, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()四、图像金字塔 通常我们通常处理恒定大小的图像。但在某些情况下我们需要处理不同分辨率的相同的图像。例如当在图像中搜索某个东西时比如人脸我们不确定物体将在该图像中出现在什么大小。在这种情况下我们将需要创建一组具有不同分辨率的相同图像并在所有这些图像中搜索对象。这些不同分辨率的图像集被称为图像金字塔因为当它们被保存在一个堆栈中顶部是最高分辨率的图像时它看起来就像一个金字塔。 金字塔的一个应用是图像混合。 4.1 高斯金字塔 import cv2 as cv import numpy as np from matplotlib import pyplot as plt plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()# 模糊图像向下采样img_down cv.pyrDown(img)img_down_down cv.pyrDown(img_down)img_down_down_down cv.pyrDown(img_down_down)# 显示图像titles [origin , 向下采样1, 向下采样2, 向下采样3]images [img, img_down, img_down_down, img_down_down_down]for i in range(len(images)):plt.subplot(2, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()4.2 拉普拉斯金字塔 拉普拉斯金字塔是由高斯金字塔形成的。拉普拉斯金字塔图像像边缘图像它的大多数元素都是零常被用于图像压缩。拉普拉斯金字塔的水平是由高斯金字塔的水平与高斯金字塔的扩展水平的差异形成的 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()# pyrDown和pyrUp并不是可逆的img_down cv.pyrDown(img)img_down_up cv.pyrUp(img_down)img_laplacian img - img_down_up# 显示图像titles [origin , img_laplacian]images [img, img_laplacian]for i in range(len(images)):plt.subplot(1, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()五、形态学 5.1 腐蚀 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(j.png)assert img is not None, file could not be read, check with os.path.exists()kernel3 np.ones((3, 3), np.uint8)img_erode_3 cv.erode(img, kernel3, iterations1)kernel5 np.ones((5, 5), np.uint8)img_erode_5 cv.erode(img, kernel5, iterations1)cross cv.getStructuringElement(cv.MORPH_CROSS, (5, 5))img_erode_by_cross cv.erode(img, cross)ellipse cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))img_erode_by_ellipse cv.erode(img, ellipse)# 显示图像titles [origin , erode_3, erode_5, cross, ellipse]images [img, img_erode_3, img_erode_5,img_erode_by_cross, img_erode_by_ellipse]for i in range(len(images)):plt.subplot(2, 3, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()5.2 膨胀 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(j.png)assert img is not None, file could not be read, check with os.path.exists()kernel3 np.ones((3, 3), np.uint8)img_dilate_3 cv.dilate(img, kernel3, iterations1)kernel5 np.ones((5, 5), np.uint8)img_dilate_5 cv.dilate(img, kernel5, iterations1)cross cv.getStructuringElement(cv.MORPH_CROSS, (5, 5))img_dilate_by_cross cv.dilate(img, cross)ellipse cv.getStructuringElement(cv.MORPH_ELLIPSE, (5, 5))img_dilate_by_ellipse cv.dilate(img, ellipse)# 显示图像titles [origin , dilate_3, dilate_5, cross, ellipse]images [img, img_dilate_3, img_dilate_5,img_dilate_by_cross, img_dilate_by_ellipse]for i in range(len(images)):plt.subplot(2, 3, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()5.3 运算 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(j.png)assert img is not None, file could not be read, check with os.path.exists()# 形态学通常用于二值化图像# 开操作先腐蚀后膨胀# 闭操作先膨胀后腐蚀# 形态学梯度膨胀减去腐蚀。# 顶帽原图像与开操作图像之间的差值图像。# 黑帽闭操作图像与原图像之间的差值图像。kernel np.ones((3, 3), np.uint8)img_opening cv.morphologyEx(img, cv.MORPH_OPEN, kernel)img_closing cv.morphologyEx(img, cv.MORPH_CLOSE, kernel)img_gradient cv.morphologyEx(img, cv.MORPH_GRADIENT, kernel)img_tophat cv.morphologyEx(img, cv.MORPH_TOPHAT, kernel)img_blackhat cv.morphologyEx(img, cv.MORPH_BLACKHAT, kernel)# 显示图像titles [origin , open, close, gradient, tophat, blackhat]images [img, img_opening, img_closing,img_gradient, img_tophat, img_blackhat]for i in range(len(images)):plt.subplot(2, 3, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()六、直方图 6.1 计算 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()img_gray cv.cvtColor(img, cv.COLOR_BGR2GRAY)img_hist cv.calcHist([img], [0], None, [256], [0, 256])# 显示图像titles [gray, origin]images [img_gray, img]for i in range(len(images)):plt.subplot(2, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.subplot(2, 2, 3)plt.plot(img_hist)plt.title(histogram)plt.xlim([0, 256])color (b, g, r)plt.subplot(2, 2, 4)for i, col in enumerate(color):histr cv.calcHist([img], [i], None, [256], [0, 256])plt.plot(histr, colorcol)plt.xlim([0, 256])plt.title(color histogram)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()6.2 均衡 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()img_gray cv.cvtColor(img, cv.COLOR_BGR2GRAY)img_hist cv.calcHist([img_gray], [0], None, [256], [0, 256])# 均衡灰度图像img_gray_equ cv.equalizeHist(img_gray)img_equ_hist cv.calcHist([img_gray_equ], [0], None, [256], [0, 256])clahe cv.createCLAHE(clipLimit2.0, tileGridSize(8, 8))img_clahe clahe.apply(img_gray)# 显示图像titles [origin , gray, equ, clahe]images [img, img_gray, img_gray_equ, img_clahe]for i in range(len(images)):plt.subplot(3, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.subplot(3, 2, 5)plt.plot(img_hist)plt.title(orgin hist)plt.xlim([0, 256])plt.subplot(3, 2, 6)plt.plot(img_equ_hist)plt.title(equ hist)plt.xlim([0, 256])plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()6.3 反向投影 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(lena.png)img_2 cv.imread(lena.png)assert img is not None, file could not be read, check with os.path.exists()img_gray cv.cvtColor(img, cv.COLOR_BGR2GRAY)img_hsv cv.cvtColor(img, cv.COLOR_BGR2HSV)img_hist cv.calcHist([img_hsv], [0], None, [256], [0, 256])cv.normalize(img_hist, img_hist, 0, 255, cv.NORM_MINMAX)# 直方图反投影 将每个像素用概率表示 经常用于图像分割或在图像中查找感兴趣的对象, 和 camshift meanshift等算法一起使用dst cv.calcBackProject([img_hsv], [0, 1], img_hist, None, 1)# 显示图像titles [origin , gray, backProject]images [img, img_gray, dst]for i in range(len(images)):plt.subplot(2, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()七、轮廓 7.1 查找显示 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(box.jpg)img_2 img.copy()assert img is not None, file could not be read, check with os.path.exists()img_gray cv.cvtColor(img, cv.COLOR_BGR2GRAY)ret, thresh cv.threshold(img_gray, 127, 255, 0)# 进行查找轮廓的图片应该是二值化图片黑色背景中找白色物体轮廓contours, hierarchy cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)cv.drawContours(img_2, contours, -1, (0, 255, 0), 3)# cv.drawContours(img_2, contours, 3, (0, 255, 0), 3)# cnt contours[4]# cv.drawContours(img_2, [cnt], 0, (0, 255, 0), 3)# 显示图像titles [origin , binary, cornor]images [img, thresh, img_2]for i in range(len(images)):plt.subplot(2, 2, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()7.2 常用特征 import cv2 as cv import numpy as np from matplotlib import pyplot as plt # plt.rcParams[font.sans-serif] [SimHei]def main():img cv.imread(g.png)assert img is not None, file could not be read, check with os.path.exists()img_gray cv.cvtColor(img, cv.COLOR_BGR2GRAY)ret, thresh cv.threshold(img_gray, 127, 255, 0)# 进行查找轮廓的图片应该是二值化图片黑色背景中找白色物体轮廓contours, hierarchy cv.findContours(thresh, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)cnt contours[0]M cv.moments(cnt)cx int(M[m10] / M[m00])cy int(M[m01] / M[m00])print(重心:, cx, cy)area cv.contourArea(cnt)print(面积:, area)perimeter cv.arcLength(cnt, True)print(周长:, perimeter)# 轮廓近似图像img_approx img.copy()epsilon 0.01*cv.arcLength(cnt, True)approx cv.approxPolyDP(cnt, epsilon, True)cv.drawContours(img_approx, [approx], -1, (0, 255, 0), 2)# 外接矩形img_rect img.copy()x, y, w, h cv.boundingRect(cnt)cv.rectangle(img_rect, (x, y), (xw, yh), (0, 255, 0), 2)# 最小矩形img_min_rect img.copy()rect cv.minAreaRect(cnt)box cv.boxPoints(rect)box np.int0(box)cv.drawContours(img_min_rect, [box], 0, (0, 0, 255), 2)# 外接圆img_circle img.copy()(x, y), radius cv.minEnclosingCircle(cnt)center (int(x), int(y))radius int(radius)cv.circle(img_circle, center, radius, (0, 255, 0), 2)# 凸包img_hull img.copy()hull cv.convexHull(cnt)cv.drawContours(img_hull, [hull], 0, (0, 255, 0), 2)# 椭圆img_ellipse img.copy()ellipse cv.fitEllipse(cnt)cv.ellipse(img_ellipse, ellipse, (0, 255, 0), 2)# 线img_line img.copy()rows, cols img.shape[:2][vx, vy, x, y] cv.fitLine(cnt, cv.DIST_L2, 0, 0.01, 0.01)lefty int((-x*vy/vx) y)righty int(((cols-x)*vy/vx)y)cv.line(img_line, (cols-1, righty), (0, lefty), (0, 255, 0), 2)cv.drawContours(img, contours, -1, (0, 255, 0), 2)# 显示图像titles [binary, cornor, approx,rect, min_rect, circle, hull, ellipse, line]images [thresh, img, img_approx, img_rect,img_min_rect, img_circle, img_hull, img_ellipse, img_line]for i in range(len(images)):plt.subplot(3, 3, i1)plt.imshow(cv.cvtColor(images[i], cv.COLOR_BGR2RGB))plt.title(titles[i])plt.axis(off)plt.tight_layout()plt.show()cv.waitKey(0)cv.destroyAllWindows()if __name__ __main__:main()参考 https://github.com/LeBron-Jian/ComputerVisionPractice
http://www.dnsts.com.cn/news/21874.html

相关文章:

  • 哈尔滨模板建站公司wordpress 菜单字体
  • 怎么制作网站链接手机吉林省建设监理协会网站
  • 网站设计说明书范文做天然文化石的网站
  • 建设网站的功能及目的网博士自助建站系统下载
  • 鄂北局网站建设者风采网站开发 占位符
  • 怎么样做门户网站工作室官网源码
  • 企业名录搜索网站专题文档dede企业网站建设
  • 公司网站做好了还需网站域名查询系统
  • 公众号 微网站开发西宁市城乡建设网站
  • 网站的风格外贸网站建设浩森宇特
  • 建网站麻烦吗wordpress文章内多页效果
  • dw做网站有哪些用处设计官网推荐
  • 做网站被骗五千多公司网站怎么弄
  • 网站建设多少钱哪个济南兴田德润有活动吗wordpress 扫码登录
  • 网站运营托管4414站长平台
  • 关于化妆品的网页设计seo关键词优化策略
  • 遥阳科技网站建设自己做的网站能联网吗
  • 服装网站建设项目规划书目前网站建设采用什么技术
  • 网站架构优化 ampwordpress博客网站描述在哪里
  • 辛集市住房和城乡建设局网站房地产 网站 案例
  • 域名网站建设方案书模板响应式全屏网站
  • 成都网站推广如何南通网站建设策划书
  • 互联网定制产品网站三五互联网站管理登录网址
  • 旅游信息管理网站开发文件设计互动网站建设
  • 有了域名搭建网站详细步骤WordPress伪静态公告404
  • 计算机网站建设方向网站开发重点难点分析
  • 凡科做的手机网站可以导出来临沂哪里有做网站
  • 睿达科网络 网站建设wordpress 社交主题
  • 山东省建设部网站官网丰南建设局网站
  • 网站建设走无形资产阿里云企业网站怎么建设