资源简介
主要是对一张图像上的多目标区域进行切割的一系列算法,其中涉及彩色图像的阈值分割、图像二值化、双边滤波去噪、图像孔洞填充、面积阈值去噪、边缘颗粒去除、对比原图的遍历替换、目标区域的位置检测以及轮廓检测、最小外接矩形画框以及按照框的大小对框选目标进行切割。
代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Fri Oct 18 16:06:32 2019
@author: 大浪淘沙
“““
def delect(image):
for i in range(3000):
for j in range(4096):
for k in range(3):
#img[i][j][k] = 255
if int(image[i][j][1])-int(image[i][j][0]) >=22:
image[i][j] = 0
if int(image[i][j][0]) <= 20:
image[i][j] = 0
return image
img1 = delet(img)
cv2.namedWindow(‘Image‘ 0)
cv2.resizeWindow(‘Image‘ 682 500)
#在窗口中显示图像
cv2.imshow(‘Image‘ img1)
#最后还要一句代码,使窗口可以一直保持住
cv2.waitKey(0)
#整体图像分割算法
import cv2
import numpy as np
from PIL import Image
img = cv2.imread(‘K:/opencv/delet739.png‘ 0)
img1 = cv2.imread(‘K:/test_png/1.4/739.png‘)
#缩放
rows cols channels = img1.shape
#二值化
ret thresh1 = cv2.threshold(img 20 255 cv2.THRESH_BINARY)
cv2.namedWindow(‘th1‘ 0)
cv2.resizeWindow(‘th1‘ 682 500)
cv2.imshow(‘th1‘ thresh1)
cv2.waitKey(0)
#双边滤波+填充
blur = cv2.bilateralFilter(thresh1 9 80 80)
fill = blur.copy()
h w = blur.shape[: 2]
mask = np.zeros((h+2 w+2) np.uint8)
cv2.floodFill(fill mask (90 90) 255);
fill_INV = cv2.bitwise_not(fill)
fill_out = blur | fill_INV
cv2.namedWindow(‘fill_out_img‘ 0)
cv2.resizeWindow(‘fill_out_img‘ 682 500)
cv2.imshow(‘fill_out_img‘ fill_out)
#cv2.imwrite(‘K:/opencv/tianchong.png‘ fill_out)
cv2.waitKey(0)
#腐蚀膨胀
dilate=cv2.dilate(fill_out None iterations=1)
cv2.namedWindow(‘pengzhang‘ 0)
cv2.resizeWindow(‘pengzhang‘ 682 500)
cv2.imshow(‘pengzhang‘ dilate)
cv2.waitKey(0)
erode=cv2.erode(dilateNoneiterations=1)
cv2.namedWindow(‘fushi‘ 0)
cv2.resizeWindow(‘fushi‘ 682 500)
cv2.imshow(‘fushi‘ erode)
cv2.waitKey(0)
def baweraopen(imagesize):
‘‘‘
@image:单通道二值图,数据类型uint8
@size:欲去除区域大小(黑底上的白区域)
‘‘‘
output=image.copy()
nlabels labels stats centroids = cv2.connectedComponentsWithStats(image)
for i in range(1nlabels-1):
regions_size=stats[i4]
if regions_size x0=stats[i0]
y0=stats[i1]
x1=s
评论
共有 条评论