码迷,mamicode.com
首页 > 其他好文 > 详细

opencv入门———提取视频/图像中的物体

时间:2020-11-16 13:42:44      阅读:8      评论:0      收藏:0      [点我收藏+]

标签:hiera   腐蚀   arch   orm   col   获取   transform   com   遍历   

如下图:

技术图片

 

 

提取后:

技术图片

 

这里可以加载网络摄像头对视频图像进行逐帧处理动态检测,在图书馆,我手机上模拟的网络摄像头和电脑不在同一热点,这里就直接拍了张照片进行测试。

①原图片太大了,对图像缩小一点

img=cv2.imread("C:/Users/31132/Desktop/mtest.jpg")

print(img.shape)
x,y=img.shape[0],img.shape[1]
frameWidth=x//6
frameHeight=y//6
imgRsize=cv2.resize(img,(frameWidth,frameHeight))

 

②对图像进行预处理,包括转换为灰度图像,高斯模糊,边缘检测,膨胀,腐蚀等,获取图像较为清晰的轮廓

def preProcessing(img):
    imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)      #灰度图像
    imgBlur=cv2.GaussianBlur(imgGray,(5,5),1)         #高斯模糊
    imgCanny=cv2.Canny(imgBlur,200,200)         #边缘检测
    kernal=np.ones((5,5))                  
    imgDial=cv2.dilate(imgCanny,kernal,iterations=2) #膨胀
    imgThres=cv2.erode(imgDial,kernal,iterations=1)  #腐蚀

    return imgThres

效果如下:

技术图片

 

 

③绘制轮廓,检测图像位置,获取四个楞角

def getContours(img):
    coutours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    biggest=np.array([])
    maxArea=0
    for cnt in coutours:
        area=cv2.contourArea(cnt)
        if area>5000:
            # cv2.drawContours(imgcontours, cnt, -1, (0, 0, 255), 5)
            #周长
            peri=cv2.arcLength(cnt,True)
            #拟合轮廓点集
            approx=cv2.approxPolyDP(cnt,0.03*peri,True)
            if area>maxArea and len(approx)==4:
                maxArea=area
                biggest=approx
    print(biggest)
    cv2.drawContours(imgcontours, biggest, -1, (255,140,0),22)

    return  biggest

 

 ④根据四个点的大小调整位置并作透视转化

def getWarp(img,biggest):
    newbig=np.zeros_like(biggest)
    newbig[0]=biggest[1]
    newbig[1]=biggest[0]
    newbig[2]=biggest[2]
    newbig[3] = biggest[3]
    print(new,newbig)
    waitdots = np.float32(newbig)
    resultd = np.float32([[0, 0], [frameWidth, 0], [0, frameHeight], [frameWidth, frameHeight]])
    martix = cv2.getPerspectiveTransform(waitdots, resultd)
    imgout = cv2.warpPerspective(img, martix, (frameWidth, frameHeight))

    return imgout

 

 

所有代码

#day05
#加载视频,网络摄像头
# cap=cv2.VideoCapture("http://192.168.137.116:4747/video")


# cap.set(3,frameWidth)
# cap.set(4,frameHeight)
# cap.set(10,150)

def getContours(img):
    coutours,hierarchy=cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
    biggest=np.array([])
    maxArea=0
    for cnt in coutours:
        area=cv2.contourArea(cnt)
        if area>5000:
            # cv2.drawContours(imgcontours, cnt, -1, (0, 0, 255), 5)
            #周长
            peri=cv2.arcLength(cnt,True)
            #拟合轮廓点集
            approx=cv2.approxPolyDP(cnt,0.03*peri,True)
            if area>maxArea and len(approx)==4:
                maxArea=area
                biggest=approx
    print(biggest)
    cv2.drawContours(imgcontours, biggest, -1, (255,140,0),22)

    return  biggest


def preProcessing(img):
    imgGray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    imgBlur=cv2.GaussianBlur(imgGray,(5,5),1)
    imgCanny=cv2.Canny(imgBlur,200,200)
    kernal=np.ones((5,5))
    imgDial=cv2.dilate(imgCanny,kernal,iterations=2)
    imgThres=cv2.erode(imgDial,kernal,iterations=1)

    return imgThres

def getWarp(img,biggest):
    newbig=np.zeros_like(biggest)
    newbig[0]=biggest[1]
    newbig[1]=biggest[0]
    newbig[2]=biggest[2]
    newbig[3] = biggest[3]
    print(new,newbig)
    waitdots = np.float32(newbig)
    resultd = np.float32([[0, 0], [frameWidth, 0], [0, frameHeight], [frameWidth, frameHeight]])
    martix = cv2.getPerspectiveTransform(waitdots, resultd)
    imgout = cv2.warpPerspective(img, martix, (frameWidth, frameHeight))

    return imgout


#图像显示:遍历帧
# while True:
#     success,img=cap.read()
img=cv2.imread("C:/Users/31132/Desktop/mtest.jpg")

print(img.shape)
x,y=img.shape[0],img.shape[1]
frameWidth=x//6
frameHeight=y//6
imgRsize=cv2.resize(img,(frameWidth,frameHeight))
imgcontours=imgRsize.copy()
imgThres=preProcessing(imgRsize)
biggest=getContours(imgThres)

imgOut=getWarp(imgRsize,biggest)
cv2.imshow("Video",imgOut)
cv2.waitKey(0)

 

opencv入门———提取视频/图像中的物体

标签:hiera   腐蚀   arch   orm   col   获取   transform   com   遍历   

原文地址:https://www.cnblogs.com/XiaoGao128/p/13955328.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!