计算机视觉:图像分类定位(单一目标检测)python实现
前言
目標(biāo)檢測:我們不僅要用算法判斷圖片中是不是貓還是狗, 還要在圖片中標(biāo)記出它的位置, 用邊框或紅色方框把貓狗圈起來, 這就是目標(biāo)檢測問題。其中“定位”的意思是判斷貓狗在圖片中的具體位置。
目標(biāo)檢測有兩類任務(wù):單一目標(biāo) ,多目標(biāo)。
能力差,電氣專業(yè),又未怎么深入研究cv.
 所以本文先探討單一目標(biāo)。
 
HOG+SVM實現(xiàn)行人檢測
先講解 opencv自帶的行人檢測例子
 HOG原理見
 計算機(jī)視覺:圖像特征與描述大全 ,有代碼(一篇博文帶你簡單了解完圖像特征提取技術(shù))
不多說,上代碼
import cv2 as cv# 讀取圖像 src = cv.imread("duoren.jpg") cv.imshow("input", src) # HOG + SVM hog = cv.HOGDescriptor() hog.setSVMDetector(cv.HOGDescriptor_getDefaultPeopleDetector()) # Detect people in the image (rects, weights) = hog.detectMultiScale(src,winStride=(4, 4), padding=(8, 8),scale=1.25,useMeanshiftGrouping=False) # 矩形框 for (x, y, w, h) in rects:cv.rectangle(src, (x, y), (x + w, y + h), (0, 255, 0), 2) # 顯示[添加鏈接描述](https://blog.csdn.net/kobeyu652453/article/details/107382227) cv.imshow("result", src) cv.waitKey(0) cv.destroyAllWindows()圖像定位實現(xiàn)
python +keras實現(xiàn)圖像分類(入門級例子講解)
 opencv進(jìn)階學(xué)習(xí)筆記12:輪廓發(fā)現(xiàn)和對象測量
目標(biāo)檢測算法很復(fù)雜。
 我嘗試用 圖像分類+對象測量 來實現(xiàn)單目標(biāo)的圖像檢測。
圖像分類 對象測量 不多說了,參考上面給的鏈接。
1讀取圖片并去噪
import cv2 as cvimage= cv.imread("catdog/dog/dog.77.jpg") image=cv.resize(image,None,fx=0.5,fy=0.5) blurred = cv.GaussianBlur(image, (5, 5), 0) # 去噪2二值化圖像
gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY)ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU)3繪制輪廓邊緣
contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)cv2.findContours()函數(shù)第一個參數(shù)是要檢索的圖片,必須是為二值圖,即黑白的(不是灰度圖),所以讀取的圖像要先轉(zhuǎn)成灰度的,再轉(zhuǎn)成二值圖,
 參數(shù)講解
 contours,hierarchy=cv2.findContours(image,mode,method)
 contours:輪廓
 hierarchy:圖像的拓?fù)湫畔?#xff08;輪廓層次)(存儲上一個輪廓,父輪廓…)
 image:二值圖像
 mode:輪廓檢索方式
 method:輪廓的近似方法
4求得包含點集最小面積的矩形,這個矩形是可以有偏轉(zhuǎn)角度的,可以與圖像的邊界不平行。
c = sorted(contours, key=cv.contourArea, reverse=True)[0]rect = cv.minAreaRect(c) box = np.int0( cv.boxPoints(rect))# draw a bounding box arounded the detected barcode and display the image cv.drawContours(image, [box], -1, (0, 255, 0), 3)講解
double cvContourArea( const CvArr* contour, CvSlice slice=CV_WHOLE_SEQ );
 contour:輪廓(頂點的序列或數(shù)組)。
 slice:感興趣區(qū)輪廓部分的起點和終點,默認(rèn)計算整個輪廓的面積。
c = sorted(contours, key=cv.contourArea, reverse=True)[0]
 取出最大的輪廓面積,有些輪廓為噪聲。
 最大輪廓一般情況下能取到我們想要的目標(biāo)物。
minAreaRect函數(shù)返回矩形的中心點坐標(biāo),長寬,旋轉(zhuǎn)角度[-90,0),當(dāng)矩形水平或豎直時均返回-90
使用cv2.boxPoints()可獲取該矩形的四個頂點坐標(biāo)。 浮點型數(shù)據(jù)
np.int0 取整
r=cv2.drawContours(image, contours, contourIdx, color[, thickness])
 r:目標(biāo)圖像
 image:原始圖像
 contours: 所有的輸入輪廓邊緣數(shù)組
 contourIdx :需要繪制的邊緣索引,如果全部繪制為-1。如果有多個目標(biāo),可以繪制第一個目標(biāo)0,第二個目標(biāo)1,第三個目標(biāo)2.。。
 color:繪制的顏色,為BGR格式的SCalar
 thickness:可選,繪制的密度,即輪廓的畫筆粗細(xì)
5找出四個頂點的x,y坐標(biāo)的最大最小值。矩形框的高=maxY-minY,寬=maxX-minX。
由于前面的提到的 包含點集最小面積的矩形 有的矩形不與圖像平行,是斜著的,如下圖。我們調(diào)整矩形框。
 
所有代碼
import cv2 as cv import numpy as np src= cv.imread("dog.16.jpg") src=cv.resize(src,None,fx=0.5,fy=0.5) image=src.copy() #去噪 blurred = cv.GaussianBlur(image, (5, 5), 0) # 去噪 #灰度轉(zhuǎn)換 gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY) #二值化 ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) #輪廓發(fā)現(xiàn) contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) #取出最大輪廓 c = sorted(contours, key=cv.contourArea, reverse=True)[0] #找到最大輪廓的最小外接矩形 rect = cv.minAreaRect(c) #取出最小外接矩形的四個頂點 box = np.int0( cv.boxPoints(rect)) #繪制矩形框 Xs = [i[0] for i in box] Ys = [i[1] for i in box] x1 = min(Xs) x2 = max(Xs) y1 = min(Ys) y2 = max(Ys) hight = y2 - y1 width = x2 - x1 cropImg = image[y1:y1 + hight, x1:x1 + width] cv.rectangle(image, (x1, y1), (x1 + width, y1 + hight), (0, 0, 255), 2) # 在原圖上,給輪廓繪制矩形 #顯示 cv.imshow("input image", src) cv.imshow('result', image)cv.waitKey(0) cv.destroyAllWindows()圖像分類定位實現(xiàn)
我應(yīng)用圖像分類 加前面提到的定位 結(jié)合起來做 單目標(biāo)的圖像監(jiān)測。
圖像分類前面給出了鏈接,這里不再給啦,博文太多鏈接了,會被顯示待審核。
PYQT 封裝吧。
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # @Author: yudengwu # @Date : 2020/8/1 import sys from PyQt5 import QtWidgets, QtCore, QtGui from PyQt5.QtGui import * from PyQt5.QtWidgets import * from PyQt5.QtCore import *import cv2 import keras from keras .models import load_model import numpy as np import re class picture(QWidget):def __init__(self):super(picture, self).__init__()self.resize(600, 400)self.setWindowTitle("貓狗分類")self.btn = QPushButton()self.btn.setText("打開圖片")self.btn.clicked.connect(self.openimage)self.label = QLabel()self.label.setText('圖片路徑')self.labelimage = QLabel()self.labelimage.setText("顯示圖片")#self.labelimage.setFixedSize(500, 400)#設(shè)置尺寸self.labelimage.setStyleSheet("QLabel{background:white;}""QLabel{color:rgb(300,300,300,120);font-size:10px;font-weight:bold;font-family:宋體;}")#預(yù)測按鈕self.btnclass=QPushButton()self.btnclass.setText('點擊預(yù)測分類')self.btnclass.clicked.connect(self.fenlei)self.labelclass=QLabel()self.labelclass.setText('預(yù)測類別')self.labelclass.setStyleSheet("font:16pt '楷體';border-width:2px;border-style: inset;border-color:gray")layout1=QVBoxLayout()layout1.addWidget(self.btn)layout1.addWidget(self.label)layout1.addWidget(self.labelimage)layout2 = QVBoxLayout()layout2.addWidget(self.btnclass)layout2.addWidget(self.labelclass)layout=QVBoxLayout()layout.addLayout(layout1)layout.addLayout(layout2)self.setLayout(layout)def openimage(self):imgName, imgType = QFileDialog.getOpenFileName(self, "打開圖片", "", "*.jpg;;*.png;;All Files(*)")#jpg = QtGui.QPixmap(imgName).scaled(self.labelimage.width(), self.label.height())#適應(yīng)labelimage尺寸,前提是label設(shè)置了尺寸jpg = QtGui.QPixmap(imgName)self.labelimage.setPixmap(jpg)self.label.setText(str(imgName))def fenlei(self):biaoqian = {'1': '貓', '0': '狗'}path=self.label.text()newName = re.sub('(D:/機(jī)器學(xué)習(xí)/學(xué)習(xí)草稿/)','', path)#print(newName)img = cv2.imread(str(newName))img = cv2.resize(img, (100, 100)) # 使尺寸大小一樣img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)img = np.array(img) / 255img = img.astype(np.float64)img = img.reshape(-1, 100, 100, 1)model = load_model('貓狗分類.h5')predict_y = model.predict(img)pred_y = int(np.round(predict_y))#print(pred_y)self.labelclass.setText(biaoqian[str(pred_y)])########圖像定位src = cv2.imread(str(newName))src = cv2.resize(src, None, fx=0.5, fy=0.5)image = src.copy()# 去噪blurred = cv2.GaussianBlur(image, (5, 5), 0) # 去噪# 灰度轉(zhuǎn)換gray = cv2.cvtColor(blurred, cv2.COLOR_BGR2GRAY)# 二值化ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)# 輪廓發(fā)現(xiàn)contours, hireachy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)# 取出最大輪廓c = sorted(contours, key=cv2.contourArea, reverse=True)[0]# 找到最大輪廓的最小外接矩形rect = cv2.minAreaRect(c)# 取出最小外接矩形的四個頂點box = np.int0(cv2.boxPoints(rect))# 繪制矩形框Xs = [i[0] for i in box]Ys = [i[1] for i in box]x1 = min(Xs)x2 = max(Xs)y1 = min(Ys)y2 = max(Ys)hight = y2 - y1width = x2 - x1cropImg = image[y1:y1 + hight, x1:x1 + width]cv2.rectangle(image, (x1, y1), (x1 + width, y1 + hight), (0, 0, 255), 2) # 在原圖上,給輪廓繪制矩形#顯示在lableimage上res = imageres = cv2.resize(res, (400, 300), interpolation=cv2.INTER_CUBIC) # 用cv2.resize設(shè)置圖片大小img2 = cv2.cvtColor(res, cv2.COLOR_BGR2RGB) # opencv讀取的bgr格式圖片轉(zhuǎn)換成rgb格式_image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3,QtGui.QImage.Format_RGB888) # pyqt5轉(zhuǎn)換成自己能放的圖片格式jpg_out = QtGui.QPixmap(_image) # 轉(zhuǎn)換成QPixmapself.labelimage.setPixmap(jpg_out) # 設(shè)置圖片顯示cv2.waitKey()cv2.destroyAllWindows()if __name__ == "__main__":app = QtWidgets.QApplication(sys.argv)my = picture()my.show()sys.exit(app.exec_())說明:
model = load_model(‘貓狗分類.h5’)
 導(dǎo)入訓(xùn)練好的分類模型
在PYQT中顯示opencv圖 核心代碼
def setImage(self):img = cv2.imread('test.jpg') #opencv讀取圖片img2 = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) #opencv讀取的bgr格式圖片轉(zhuǎn)換成rgb格式_image = QtGui.QImage(img2[:], img2.shape[1], img2.shape[0], img2.shape[1] * 3, QtGui.QImage.Format_RGB888) #pyqt5轉(zhuǎn)換成自己能放的圖片格式j(luò)pg_out = QtGui.QPixmap(_image).scaled(self.imgLabel.width(), self.imgLabel.height()) #設(shè)置圖片大小self.imgLabel.setPixmap(jpg_out) #設(shè)置圖片顯示結(jié)果演示
本文給出的方法不是純粹的目標(biāo)檢測算法。定位有的圖有所缺陷。
只是圖像分類+對象測量 來實現(xiàn)單一目標(biāo)檢測功能
等我有時間研究下目標(biāo)檢測算法后,再來寫博文。
電氣專業(yè)的計算機(jī)萌新:余登武,寫博文不容易,如果你覺得本文對你有用,請點個贊支持下,謝謝。
總結(jié)
以上是生活随笔為你收集整理的计算机视觉:图像分类定位(单一目标检测)python实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 未激活信用卡注销流程
- 下一篇: SpaceX原型机着陆数分钟后爆炸
