『壹』 python下使用openCV3,如何在一幅灰度圖中,為所有灰度為某特定值的點賦另一灰度值
你好,我覺得用np.where是可以實現的,下面是相關的實現代碼:
importcv2
importnumpyasnp
image=np.zeros((400,400,3),dtype="uint8")
raw=image.()
image[np.where((image==[0,0,0]).all(axis=2))]=[255,255,255]
cv2.imshow('Test0',image)
lower_black=np.array([0,0,0],dtype="uint16")
upper_black=np.array([70,70,70],dtype="uint16")
black_mask=cv2.inRange(image,lower_black,upper_black)
image[np.where((image==[0,0,0]).all(axis=2))]=[155,255,155]
black_mask[np.where(black_mask==[0])]=[155]
cv2.imshow('Test',image)
cv2.imshow('Test2',raw)
cv2.imshow('Test3',black_mask)
『貳』 opencv中有幾個函數是什麼意思
1、cvLoadImage:將圖像文件載入至內存;
2、cvNamedWindow:在屏幕上創建一個窗口;
3、cvShowImage:在一個已創建好的窗口中顯示圖像;
4、cvWaitKey:使程序暫停,等待用戶觸發一個按鍵操作;
5、cvReleaseImage:釋放圖像文件所分配的內存;
6、cvDestroyWindow:銷毀顯示圖像文件的窗口;
7、cvCreateFileCapture:通過參數設置確定要讀入的AVI文件;
8、cvQueryFrame:用來將下一幀視頻文件載入內存;
9、cvReleaseCapture:釋放CvCapture結構開辟的內存空間;
10、cvCreateTrackbar:創建一個滾動條;
11、cvSetCaptureProperty:設置CvCapture對象的各種屬性;
12、cvGetCaptureProperty:查詢CvCapture對象的各種屬性;
13、cvGetSize:當前圖像結構的大小;
14、cvSmooth:對圖像進行平滑處理;
15、cvPyrDown:圖像金字塔,降采樣,圖像縮小為原來四分之一;
16、cvCanny:Canny邊緣檢測;
17、cvCreateCameraCapture:從攝像設備中讀入數據;
18、cvCreateVideoWriter:創建一個寫入設備以便逐幀將視頻流寫入視頻文件;
19、cvWriteFrame:逐幀將視頻流寫入文件;
20、cvReleaseVideoWriter:釋放CvVideoWriter結構開辟的內存空間;
21、CV_MAT_ELEM:從矩陣中得到一個元素;
22、cvAbs:計算數組中所有元素的絕對值;
23、cvAbsDiff:計算兩個數組差值的絕對值;
24、cvAbsDiffS:計算數組和標量差值的絕對值;
25、cvAdd:兩個數組的元素級的加運算;
26、cvAddS:一個數組和一個標量的元素級的相加運算;
27、cvAddWeighted:兩個數組的元素級的加權相加運算(alpha運算);
28、cvAvg:計算數組中所有元素的平均值;
29、cvAvgSdv:計算數組中所有元素的絕對值和標准差;
30、cvCalcCovarMatrix:計算一組n維空間向量的協方差;
31、cvCmp:對兩個數組中的所有元素運用設置的比較操作;
32、cvCmpS:對數組和標量運用設置的比較操作;
33、cvConvertScale:用可選的縮放值轉換數組元素類型;
34、cvCopy:把數組中的值復制到另一個數組中;
35、cvCountNonZero:計算數組中非0值的個數;
36、cvCrossProct:計算兩個三維向量的向量積(叉積);
37、cvCvtColor:將數組的通道從一個顏色空間轉換另外一個顏色空間;
38、cvDet:計算方陣的行列式;
39、cvDiv:用另外一個數組對一個數組進行元素級的除法運算;
40、cvDotProct:計算兩個向量的點積;
41、cvEigenVV:計算方陣的特徵值和特徵向量;
42、cvFlip:圍繞選定軸翻轉;
43、cvGEMM:矩陣乘法;
44、cvGetCol:從一個數組的列中復制元素;
45、cvGetCols:從數據的相鄰的多列中復制元素;
46、cvGetDiag:復制數組中對角線上的所有元素;
47、cvGetDims:返回數組的維數;
48、cvGetDimSize:返回一個數組的所有維的大小;
49、cvGetRow:從一個數組的行中復制元素值;
50、cvGetRows:從一個數組的多個相鄰的行中復制元素值;
51、cvGetSize:得到二維的數組的尺寸,以CvSize返回;
52、cvGetSubRect:從一個數組的子區域復制元素值;
53、cvInRange:檢查一個數組的元素是否在另外兩個數組中的值的范圍內;
54、cvInRangeS:檢查一個數組的元素的值是否在另外兩個標量的范圍內;
55、cvInvert:求矩陣的逆;
56、cvMahalonobis:計算兩個向量間的馬氏距離;
57、cvMax:在兩個數組中進行元素級的取最大值操作;
58、cvMaxS:在一個數組和一個標量中進行元素級的取最大值操作;
59、cvMerge:把幾個單通道圖像合並為一個多通道圖像;
60、cvMin:在兩個數組中進行元素級的取最小值操作;
61、cvMinS:在一個數組和一個標量中進行元素級的取最小值操作;
62、cvMinMaxLoc:尋找數組中的最大最小值;
63、cvMul:計算兩個數組的元素級的乘積(點乘);
64、cvNot:按位對數組中的每一個元素求反;
65、cvNormalize:將數組中元素進行歸一化;
66、cvOr:對兩個數組進行按位或操作;
67、cvOrs:在數組與標量之間進行按位或操作;
68、cvRece:通過給定的操作符將二維數組簡為向量;
69、cvRepeat:以平鋪的方式進行數組復制;
70、cvSet:用給定值初始化數組;
71、cvSetZero:將數組中所有元素初始化為0;
72、cvSetIdentity:將數組中對角線上的元素設為1,其他置0;
73、cvSolve:求出線性方程組的解;
74、cvSplit:將多通道數組分割成多個單通道數組;
75、cvSub:兩個數組元素級的相減;
76、cvSubS:元素級的從數組中減去標量;
77、cvSubRS:元素級的從標量中減去數組;
78、cvSum:對數組中的所有元素求和;
79、cvSVD:二維矩陣的奇異值分解;
80、cvSVBkSb:奇異值回代計算;
81、cvTrace:計算矩陣跡;
82、cvTranspose:矩陣的轉置運算;
83、cvXor:對兩個數組進行按位異或操作;
84、cvXorS:在數組和標量之間進行按位異或操作;
85、cvZero:將所有數組中的元素置為0;
86、cvConvertScaleAbs:計算可選的縮放值的絕對值之後再轉換數組元素的類型;
87、cvNorm:計算數組的絕對范數, 絕對差分范數或者相對差分范數;
88、cvAnd:對兩個數組進行按位與操作;
89、cvAndS:在數組和標量之間進行按位與操作;
90、cvScale:是cvConvertScale的一個宏,可以用來重新調整數組的內容,並且可以將參數從一種數
據類型轉換為另一種;
91、cvT:是函數cvTranspose的縮寫;
92、cvLine:畫直線;
93、cvRectangle:畫矩形;
94、cvCircle:畫圓;
95、cvEllipse:畫橢圓;
96、cvEllipseBox:使用外接矩形描述橢圓;
97、cvFillPoly、cvFillConvexPoly、cvPolyLine:畫多邊形;
98、cvPutText:在圖像上輸出一些文本;
99、cvInitFont:採用一組參數配置一些用於屏幕輸出的基本個特定字體;
100、cvSave:矩陣保存;
101、cvLoad:矩陣讀取;
102、cvOpenFileStorage:為讀/寫打開存儲文件;
103、cvReleaseFileStorage:釋放存儲的數據;
104、cvStartWriteStruct:開始寫入新的數據結構;
105、cvEndWriteStruct:結束寫入數據結構;
106、cvWriteInt:寫入整數型;
107、cvWriteReal:寫入浮點型;
108、cvWriteString:寫入字元型;
109、cvWriteComment:寫一個XML或YAML的注釋字串;
110、cvWrite:寫一個對象;
111、cvWriteRawData:寫入多個數值;
112、cvWriteFileNode:將文件節點寫入另一個文件存儲器;
113、cvGetRootFileNode:獲取存儲器最頂層的節點;
114、cvGetFileNodeByName:在映圖或存儲器中找到相應節點;
115、cvGetHashedKey:為名稱返回一個惟一的指針;
116、cvGetFileNode:在映圖或文件存儲器中找到節點;
117、cvGetFileNodeName:返迴文件的節點名;
118、cvReadInt:讀取一個無名稱的整數型;
119、cvReadIntByName:讀取一個有名稱的整數型;
120、cvReadReal:讀取一個無名稱的浮點型;
121、cvReadRealByName:讀取一個有名稱的浮點型;
122、cvReadString:從文件節點中尋找字元串;
123、cvReadStringByName:找到一個有名稱的文件節點並返回它;
124、cvRead:將對象解碼並返回它的指針;
125、cvReadByName:找到對象並解碼;
126、cvReadRawData:讀取多個數值;
127、cvStartReadRawData:初始化文件節點序列的讀取;
128、cvReadRawDataSlice:讀取文件節點的內容;
129、cvGetMoleInfo:檢查IPP庫是否已經正常安裝並且檢驗運行是否正常;
130、cvResizeWindow:用來調整窗口的大小;
131、cvSaveImage:保存圖像;
132、cvMoveWindow:將窗口移動到其左上角為x,y的位置;
133、cvDestroyAllWindow:用來關閉所有窗口並釋放窗口相關的內存空間;
134、cvGetTrackbarPos:讀取滑動條的值;
135、cvSetTrackbarPos:設置滑動條的值;
136、cvGrabFrame:用於快速將視頻幀讀入內存;
137、cvRetrieveFrame:對讀入幀做所有必須的處理;
138、cvConvertImage:用於在常用的不同圖像格式之間轉換;
139、cvErode:形態腐蝕;
140、cvDilate:形態學膨脹;
141、cvMorphologyEx:更通用的形態學函數;
142、cvFloodFill:漫水填充演算法,用來進一步控制哪些區域將被填充顏色;
143、cvResize:放大或縮小圖像;
144、cvPyrUp:圖像金字塔,將現有的圖像在每個維度上都放大兩倍;
145、cvPyrSegmentation:利用金字塔實現圖像分割;
146、cvThreshold:圖像閾值化;
147、cvAcc:可以將8位整數類型圖像累加為浮點圖像;
148、cvAdaptiveThreshold:圖像自適應閾值;
149、cvFilter2D:圖像卷積;
150、cvCopyMakeBorder:將特定的圖像輕微變大,然後以各種方式自動填充圖像邊界;
151、cvSobel:圖像邊緣檢測,Sobel運算元;
152、cvLaplace:拉普拉斯變換、圖像邊緣檢測;
153、cvHoughLines2:霍夫直線變換;
154、cvHoughCircles:霍夫圓變換;
155、cvRemap:圖像重映射,校正標定圖像,圖像插值;
156、cvWarpAffine:稠密仿射變換;
157、cvGetQuadrangleSubPix:仿射變換;
158、cvGetAffineTransform:仿射映射矩陣的計算;
159、cvCloneImage:將整個IplImage結構復制到新的IplImage中;
160、cv2DRotationMatrix:仿射映射矩陣的計算;
161、cvTransform:稀疏仿射變換;
162、cvWarpPerspective:密集透視變換(單應性);
163、cvGetPerspectiveTransform:計算透視映射矩陣;
164、cvPerspectiveTransform:稀疏透視變換;
165、cvCartToPolar:將數值從笛卡爾空間到極坐標(極性空間)進行映射;
166、cvPolarToCart:將數值從極性空間到笛卡爾空間進行映射;
167、cvLogPolar:對數極坐標變換;
168、cvDFT:離散傅里葉變換;
169、cvMulSpectrums:頻譜乘法;
170、cvDCT:離散餘弦變換;
171、cvIntegral:計算積分圖像;
172、cvDistTransform:圖像的距離變換;
173、cvEqualizeHist:直方圖均衡化;
174、cvCreateHist:創建一新直方圖;
175、cvMakeHistHeaderForArray:根據已給出的數據創建直方圖;
176、cvNormalizeHist:歸一化直方圖;
177、cvThreshHist:直方圖閾值函數;
178、cvCalcHist:從圖像中自動計算直方圖;
179、cvCompareHist:用於對比兩個直方圖的相似度;
180、cvCalcEMD2:陸地移動距離(EMD)演算法;
181、cvCalcBackProject:反向投影;
182、cvCalcBackProjectPatch:圖塊的方向投影;
183、cvMatchTemplate:模板匹配;
184、cvCreateMemStorage:用於創建一個內存存儲器;
185、cvCreateSeq:創建序列;
186、cvSeqInvert:將序列進行逆序操作;
187、cvCvtSeqToArray:復制序列的全部或部分到一個連續內存數組中;
188、cvFindContours:從二值圖像中尋找輪廓;
189、cvDrawContours:繪制輪廓;
190、cvApproxPoly:使用多邊形逼近一個輪廓;
191、cvContourPerimeter:輪廓長度;
192、cvContoursMoments:計算輪廓矩;
193、cvMoments:計算Hu不變矩;
194、cvMatchShapes:使用矩進行匹配;
195、cvInitLineIterator:對任意直線上的像素進行采樣;
196、cvSampleLine:對直線采樣;
197、cvAbsDiff:幀差;
198、cvWatershed:分水嶺演算法;
199、cvInpaint:修補圖像;
200、cvGoodFeaturesToTrack:尋找角點;
201、cvFindCornerSubPix:用於發現亞像素精度的角點位置;
202、cvCalcOpticalFlowLK:實現非金字塔的Lucas-Kanade稠密光流演算法;
203、cvMeanShift:mean-shift跟蹤演算法;
204、cvCamShift:camshift跟蹤演算法;
205、cvCreateKalman:創建Kalman濾波器;
206、cvCreateConDensation:創建condensation濾波器;
207、cvConvertPointsHomogenious:對齊次坐標進行轉換;
208、cvFindChessboardCorners:定位棋盤角點;
209、cvFindHomography:計算單應性矩陣;
210、cvRodrigues2:羅德里格斯變換;
211、cvFitLine:直線擬合演算法;
212、cvCalcCovarMatrix:計算協方差矩陣;
213、cvInvert:計算協方差矩陣的逆矩陣;
214、cvMahalanobis:計算Mahalanobis距離;
215、cvKMeans2:K均值;
216、cvCloneMat:根據一個已有的矩陣創建一個新矩陣;
217、cvPreCornerDetect:計算用於角點檢測的特徵圖;
218、cvGetImage:CvMat圖像數據格式轉換成IplImage圖像數據格式;
219、cvMatMul:兩矩陣相乘;
『叄』 如何用opencv實現任意聯通區域的最大內接圓
代碼 1)Two-pass演算法的一種實現說明:基於OpenCV和C++實現,領域:4-領域。實現與演算法描述稍有差別(具體為記錄具有相等關系的label方法實現上)。 // Connected Component Analysis/Labeling By Two-Pass Algorithm // Author: www.icvpr.com // Blog : http://blog.csdn.net/icvpr #include <iostream> #include <string> #include <list> #include <vector> #include <map> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> void icvprCcaByTwoPass(const cv::Mat& _binImg, cv::Mat& _lableImg) { // connected component analysis (4-component) // use two-pass algorithm // 1. first pass: label each foreground pixel with a label // 2. second pass: visit each labeled pixel and merge neighbor labels // // foreground pixel: _binImg(x,y) = 1 // background pixel: _binImg(x,y) = 0 if (_binImg.empty() || _binImg.type() != CV_8UC1) { return ; } // 1. first pass _lableImg.release() ; _binImg.convertTo(_lableImg, CV_32SC1) ; int label = 1 ; // start by 2 std::vector<int> labelSet ; labelSet.push_back(0) ; // background: 0 labelSet.push_back(1) ; // foreground: 1 int rows = _binImg.rows - 1 ; int cols = _binImg.cols - 1 ; for (int i = 1; i < rows; i++) { int* data_preRow = _lableImg.ptr<int>(i-1) ; int* data_curRow = _lableImg.ptr<int>(i) ; for (int j = 1; j < cols; j++) { if (data_curRow[j] == 1) { std::vector<int> neighborLabels ; neighborLabels.reserve(2) ; int leftPixel = data_curRow[j-1] ; int upPixel = data_preRow[j] ; if ( leftPixel > 1) { neighborLabels.push_back(leftPixel) ; } if (upPixel > 1) { neighborLabels.push_back(upPixel) ; } if (neighborLabels.empty()) { labelSet.push_back(++label) ; // assign to a new label data_curRow[j] = label ; labelSet[label] = label ; } else { std::sort(neighborLabels.begin(), neighborLabels.end()) ; int smallestLabel = neighborLabels[0] ; data_curRow[j] = smallestLabel ; // save equivalence for (size_t k = 1; k < neighborLabels.size(); k++) { int tempLabel = neighborLabels[k] ; int& oldSmallestLabel = labelSet[tempLabel] ; if (oldSmallestLabel > smallestLabel) { labelSet[oldSmallestLabel] = smallestLabel ; oldSmallestLabel = smallestLabel ; } else if (oldSmallestLabel < smallestLabel) { labelSet[smallestLabel] = oldSmallestLabel ; } } } } } } // update equivalent labels // assigned with the smallest label in each equivalent label set for (size_t i = 2; i < labelSet.size(); i++) { int curLabel = labelSet[i] ; int preLabel = labelSet[curLabel] ; while (preLabel != curLabel) { curLabel = preLabel ; preLabel = labelSet[preLabel] ; } labelSet[i] = curLabel ; } // 2. second pass for (int i = 0; i < rows; i++) { int* data = _lableImg.ptr<int>(i) ; for (int j = 0; j < cols; j++) { int& pixelLabel = data[j] ; pixelLabel = labelSet[pixelLabel] ; } } } 2)Seed-Filling種子填充方法說明:基於OpenCV和C++實現;領域:4-領域。 // Connected Component Analysis/Labeling By Seed-Filling Algorithm // Author: www.icvpr.com // Blog : http://blog.csdn.net/icvpr #include <iostream> #include <string> #include <list> #include <vector> #include <map> #include <stack> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> void icvprCcaBySeedFill(const cv::Mat& _binImg, cv::Mat& _lableImg) { // connected component analysis (4-component) // use seed filling algorithm // 1. begin with a foreground pixel and push its foreground neighbors into a stack; // 2. pop the top pixel on the stack and label it with the same label until the stack is empty // // foreground pixel: _binImg(x,y) = 1 // background pixel: _binImg(x,y) = 0 if (_binImg.empty() || _binImg.type() != CV_8UC1) { return ; } _lableImg.release() ; _binImg.convertTo(_lableImg, CV_32SC1) ; int label = 1 ; // start by 2 int rows = _binImg.rows - 1 ; int cols = _binImg.cols - 1 ; for (int i = 1; i < rows-1; i++) { int* data= _lableImg.ptr<int>(i) ; for (int j = 1; j < cols-1; j++) { if (data[j] == 1) { std::stack<std::pair<int,int>> neighborPixels ; neighborPixels.push(std::pair<int,int>(i,j)) ; // pixel position: <i,j> ++label ; // begin with a new label while (!neighborPixels.empty()) { // get the top pixel on the stack and label it with the same label std::pair<int,int> curPixel = neighborPixels.top() ; int curX = curPixel.first ; int curY = curPixel.second ; _lableImg.at<int>(curX, curY) = label ; // pop the top pixel neighborPixels.pop() ; // push the 4-neighbors (foreground pixels) if (_lableImg.at<int>(curX, curY-1) == 1) {// left pixel neighborPixels.push(std::pair<int,int>(curX, curY-1)) ; } if (_lableImg.at<int>(curX, curY+1) == 1) {// right pixel neighborPixels.push(std::pair<int,int>(curX, curY+1)) ; } if (_lableImg.at<int>(curX-1, curY) == 1) {// up pixel neighborPixels.push(std::pair<int,int>(curX-1, curY)) ; } if (_lableImg.at<int>(curX+1, curY) == 1) {// down pixel neighborPixels.push(std::pair<int,int>(curX+1, curY)) ; } } } } } } 3)顏色標記(用於顯示) // Connected Component Analysis/Labeling -- Color Labeling // Author: www.icvpr.com // Blog : http://blog.csdn.net/icvpr #include <iostream> #include <string> #include <list> #include <vector> #include <map> #include <stack> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> cv::Scalar icvprGetRandomColor() { uchar r = 255 * (rand()/(1.0 + RAND_MAX)); uchar g = 255 * (rand()/(1.0 + RAND_MAX)); uchar b = 255 * (rand()/(1.0 + RAND_MAX)); return cv::Scalar(b,g,r) ; } void icvprLabelColor(const cv::Mat& _labelImg, cv::Mat& _colorLabelImg) { if (_labelImg.empty() || _labelImg.type() != CV_32SC1) { return ; } std::map<int, cv::Scalar> colors ; int rows = _labelImg.rows ; int cols = _labelImg.cols ; _colorLabelImg.release() ; _colorLabelImg.create(rows, cols, CV_8UC3) ; _colorLabelImg = cv::Scalar::all(0) ; for (int i = 0; i < rows; i++) { const int* data_src = (int*)_labelImg.ptr<int>(i) ; uchar* data_dst = _colorLabelImg.ptr<uchar>(i) ; for (int j = 0; j < cols; j++) { int pixelValue = data_src[j] ; if (pixelValue > 1) { if (colors.count(pixelValue) <= 0) { colors[pixelValue] = icvprGetRandomColor() ; } cv::Scalar color = colors[pixelValue] ; *data_dst++ = color[0] ; *data_dst++ = color[1] ; *data_dst++ = color[2] ; } else { data_dst++ ; data_dst++ ; data_dst++ ; } } } } 4)測試程序 // Connected Component Analysis/Labeling -- Test code // Author: www.icvpr.com // Blog : http://blog.csdn.net/icvpr #include <iostream> #include <string> #include <list> #include <vector> #include <map> #include <stack> #include <opencv2/imgproc/imgproc.hpp> #include <opencv2/highgui/highgui.hpp> int main(int argc, char** argv) { cv::Mat binImage = cv::imread("../icvpr.com.jpg", 0) ; cv::threshold(binImage, binImage, 50, 1, CV_THRESH_BINARY_INV) ; // connected component labeling cv::Mat labelImg ; icvprCcaByTwoPass(binImage, labelImg) ; //icvprCcaBySeedFill(binImage, labelImg) ; // show result cv::Mat grayImg ; labelImg *= 10 ; labelImg.convertTo(grayImg, CV_8UC1) ; cv::imshow("labelImg", grayImg) ; cv::Mat colorLabelImg ; icvprLabelColor(labelImg, colorLabelImg) ; cv::imshow("colorImg", colorLabelImg) ; cv::waitKey(0) ; return 0 ; }
『肆』 如何識別圖片撲克牌數字導入表格
您可以通過OpenCV識別圖片撲克牌數字導入表格。
完成一張撲克牌的識別主要步驟有:從攝像頭獲取撲克牌圖片,二值化後查找圖片最外層輪廓,並截取出輪廓內部的圖片,即拍攝的撲克牌。使用霍夫線檢測和旋轉來標定撲克牌位置並截取,比通過查找輪廓標定更准確,對背景環境要求也更低。使用漫水填充演算法把撲克牌四周的多餘的背景變成和撲克牌牌面背景一樣的白色像素。此時圖片只剩白色背景以及黑色的撲克牌數字、花色、頭像等,再查找最左上角輪廓並截取出,這就是撲克牌的數字。從餘下的圖片中再查找最左邊的輪廓並截取出,這就是撲克牌的花色。可以將數字和花色都預先保存下來,進行一些處理,放到 KNN 里進行訓練得到預測模型。得到模型後就可以從1開始一套走下來自動預測識別撲克牌了。
OpenCV是一個基於Apache2.0許可(開源)發行的跨平台計算機視覺和機器學習軟體庫,可以運行在Linux、Windows、Android和Mac OS操作系統上。它輕量級而且高效——由一系列 C 函數和少量 C++ 類構成,同時提供了Python、Ruby、MATLAB等語言的介面,實現了圖像處理和計算機視覺方面的很多通用演算法。
『伍』 opencv畫圓方法
在原點計算圓上的點,然後利用繞固定點的旋轉公式,將其換算成新的坐標。關於二維旋轉公式,可以參見 計算機圖形學(OpenGL 版) 第三版, Donald 著,蔡世傑翻譯,電子工業出版的那本。
『陸』 1two-pass法;2seed-filling種子填充法 哪種更好
1)Two-pass演算法的一種實現說明:基於OpenCV和C++實現,領域:4-領域。實現與演算法2)Seed-Filling種子填充方法說明:基於OpenCV和C++實現;領域:4-領域。
『柒』 opencv演算法怎麼實現八鄰域的標記
用cvRectangle()函數。
OpenCV裡面的繪圖函數函數功能: 通過對角線上的兩個頂點繪制簡單、指定粗細或者帶填充的矩形函數原型:void cvRectangle( CvArr* img, CvPoint pt1, CvPoint pt2, CvScalar color,int thickness=1, int line_type=8, int shift=0 );參數介紹:img -- 圖像.pt1 -- 矩形的一個頂點。pt2 -- 矩形對角線上的另一個頂點color -- 線條顏色 (RGB) 或亮度(灰度圖像 )(grayscale image)。thickness -- 組成矩形的線條的粗細程度。取負值時(如 CV_FILLED)函數繪制填充了色彩的矩形。line_type -- 線條的類型。見cvLine的描述shift -- 坐標點的小數點位數。
『捌』 基於opencv的顏色分類
1、從攝像機獲取當前幀圖像
2、按像素判斷一個像素和周圍像素的相似度有多少(可以按兩個像素的RGB三個分量的差值的和來判斷,用區域生長演算法)
3、對整付圖像進行形態學處理,填充孔洞
4、識別各個區域的輪廓,並計算輪廓的顏色的平均值
5、按平均值和你設定的值進行比較,就可判斷出來啦
當然方法很多,這只是相對比較容易實現,實時性也比較高的方法,具體要根據軟體的要求,比如識別率,速度,顏色差異程度等
也可使用顏色直方圖直接判斷
希望能幫到你
『玖』 如何使用opencv實現金字塔光流lk跟蹤演算法
#include <stdio.h>
#include <windows.h>
#include "cv.h"
#include "cxcore.h"
#include "highgui.h"
#include <opencv2\opencv.hpp>
using namespace cv;
static const double pi = 3.14159265358979323846;
inline static double square(int a)
{
return a * a;
}
/*該函數目的:給img分配內存空間,並設定format,如位深以及channel數*/
inline static void allocateOnDemand(IplImage **img, CvSize size, int depth, int channels)
{
if (*img != NULL) return;
*img = cvCreateImage(size, depth, channels);
if (*img == NULL)
{
fprintf(stderr, "Error: Couldn't allocate image. Out of memory?\n");
exit(-1);
}
}
/*主函數,原程序是讀取avi視頻文件,然後處理,我簡單改成從攝像頭直接讀取數據*/
int main(int argc, char *argv[])
{
//讀取攝像頭
VideoCapture cap(0);
//讀取視頻文件
//VideoCapture cap; cap.open("optical_flow_input.avi");
if (!cap.isOpened())
{
return -1;
}
Mat frame;
/*
bool stop = false;
while (!stop)
{
cap >> frame;
// cvtColor(frame, edges, CV_RGB2GRAY);
// GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
// Canny(edges, edges, 0, 30, 3);
// imshow("當前視頻", edges);
imshow("當前視頻", frame);
if (waitKey(30) >= 0)
stop = true;
}
*/
//CvCapture *input_video = cvCaptureFromFile( "optical_flow_input.avi" );
//cv::VideoCapture cap = *(cv::VideoCapture *) userdata;
//if (input_video == NULL)
// {
// fprintf(stderr, "Error: Can't open video device.\n");
// return -1;
// }
/*先讀取一幀,以便得到幀的屬性,如長、寬等*/
//cvQueryFrame(input_video);
/*讀取幀的屬性*/
CvSize frame_size;
frame_size.height = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
frame_size.width = cap.get(CV_CAP_PROP_FRAME_WIDTH);
/*********************************************************/
/*用於把結果寫到文件中去,非必要
int frameW = frame_size.height; // 744 for firewire cameras
int frameH = frame_size.width; // 480 for firewire cameras
VideoWriter writer("VideoTest.avi", -1, 25.0, cvSize(frameW, frameH), true);
/*開始光流法*/
//VideoWriter writer("VideoTest.avi", CV_FOURCC('D', 'I', 'V', 'X'), 25.0, Size(640, 480), true);
while (true)
{
static IplImage *frame = NULL, *frame1 = NULL, *frame1_1C = NULL,
*frame2_1C = NULL, *eig_image = NULL, *temp_image = NULL,
*pyramid1 = NULL, *pyramid2 = NULL;
Mat framet;
/*獲取第一幀*/
// cap >> framet;
cap.read(framet);
Mat edges;
//黑白抽象濾鏡模式
// cvtColor(framet, edges, CV_RGB2GRAY);
// GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
// Canny(edges, edges, 0, 30, 3);
//轉換mat格式到lpiimage格式
frame = &IplImage(framet);
if (frame == NULL)
{
fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
return -1;
}
/*由於opencv的光流函數處理的是8位的灰度圖,所以需要創建一個同樣格式的
IplImage的對象*/
allocateOnDemand(&frame1_1C, frame_size, IPL_DEPTH_8U, 1);
/* 把攝像頭圖像格式轉換成OpenCV慣常處理的圖像格式*/
cvConvertImage(frame, frame1_1C, 0);
/* 我們需要把具有全部顏色信息的原幀保存,以備最後在屏幕上顯示用*/
allocateOnDemand(&frame1, frame_size, IPL_DEPTH_8U, 3);
cvConvertImage(frame, frame1, 0);
/* 獲取第二幀 */
//cap >> framet;
cap.read(framet);
// cvtColor(framet, edges, CV_RGB2GRAY);
// GaussianBlur(edges, edges, Size(7, 7), 1.5, 1.5);
// Canny(edges, edges, 0, 30, 3);
frame = &IplImage(framet);
if (frame == NULL)
{
fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
return -1;
}
/*原理同上*/
allocateOnDemand(&frame2_1C, frame_size, IPL_DEPTH_8U, 1);
cvConvertImage(frame, frame2_1C, 0);
/*********************************************************
開始shi-Tomasi演算法,該演算法主要用於feature selection,即一張圖中哪些是我
們感興趣需要跟蹤的點(interest point)
input:
* "frame1_1C" 輸入圖像.
* "eig_image" and "temp_image" 只是給該演算法提供可操作的內存區域.
* 第一個".01" 規定了特徵值的最小質量,因為該演算法要得到好的特徵點,哪就
需要一個選擇的閾值
* 第二個".01" 規定了像素之間最小的距離,用於減少運算復雜度,當然也一定
程度降低了跟蹤精度
* "NULL" 意味著處理整張圖片,當然你也可以指定一塊區域
output:
* "frame1_features" 將會包含fram1的特徵值
* "number_of_features" 將在該函數中自動填充上所找到特徵值的真實數目,
該值<= 400
**********************************************************/
/*開始准備該演算法需要的輸入*/
/* 給eig_image,temp_image分配空間*/
allocateOnDemand(&eig_image, frame_size, IPL_DEPTH_32F, 1);
allocateOnDemand(&temp_image, frame_size, IPL_DEPTH_32F, 1);
/* 定義存放frame1特徵值的數組,400隻是定義一個上限 */
CvPoint2D32f frame1_features[400];
int number_of_features = 400;
/*開始跑shi-tomasi函數*/
cvGoodFeaturesToTrack(frame1_1C, eig_image, temp_image,
frame1_features, &number_of_features, .01, .01, NULL);
/**********************************************************
開始金字塔Lucas Kanade光流法,該演算法主要用於feature tracking,即是算出
光流,並跟蹤目標。
input:
* "frame1_1C" 輸入圖像,即8位灰色的第一幀
* "frame2_1C" 第二幀,我們要在其上找出第一幀我們發現的特徵點在第二幀
的什麼位置
* "pyramid1" and "pyramid2" 是提供給該演算法可操作的內存區域,計算中間
數據
* "frame1_features" 由shi-tomasi演算法得到的第一幀的特徵點.
* "number_of_features" 第一幀特徵點的數目
* "optical_flow_termination_criteria" 該演算法中迭代終止的判別,這里是
epsilon<0.3,epsilon是兩幀中對應特徵窗口的光度之差的平方,這個以後的文
章會講
* "0" 這個我不知道啥意思,反正改成1就出不來光流了,就用作者原話解釋把
means disable enhancements. (For example, the second array isn't
pre-initialized with guesses.)
output:
* "frame2_features" 根據第一幀的特徵點,在第二幀上所找到的對應點
* "optical_flow_window" lucas-kanade光流演算法的運算窗口,具體lucas-kanade
會在下一篇詳述
* "5" 指示最大的金字塔層數,0表示只有一層,那就是沒用金字塔演算法
* "optical_flow_found_feature" 用於指示在第二幀中是否找到對應特徵值,
若找到,其值為非零
* "optical_flow_feature_error" 用於存放光流誤差
**********************************************************/
/*開始為pyramid lucas kanade光流演算法輸入做准備*/
CvPoint2D32f frame2_features[400];
/* 該數組相應位置的值為非零,如果frame1中的特徵值在frame2中找到 */
char optical_flow_found_feature[400];
/* 數組第i個元素表對應點光流誤差*/
float optical_flow_feature_error[400];
/*lucas-kanade光流法運算窗口,這里取3*3的窗口,可以嘗試下5*5,區別就是5*5
出現aperture problem的幾率較小,3*3運算量小,對於feature selection即shi-tomasi演算法來說足夠了*/
CvSize optical_flow_window = cvSize(5, 5);
// CvSize optical_flow_window = cvSize(5, 5);
/* 終止規則,當完成20次迭代或者當epsilon<=0.3,迭代終止,可以嘗試下別的值*/
CvTermCriteria optical_flow_termination_criteria= cvTermCriteria(CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3);
/*分配工作區域*/
allocateOnDemand(&pyramid1, frame_size, IPL_DEPTH_8U, 1);
allocateOnDemand(&pyramid2, frame_size, IPL_DEPTH_8U, 1);
/*開始跑該演算法*/
cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2,frame1_features, frame2_features, number_of_features,
optical_flow_window, 5, optical_flow_found_feature,optical_flow_feature_error, optical_flow_termination_criteria, 0);
/*畫光流場,畫圖是依據兩幀對應的特徵值,
這個特徵值就是圖像上我們感興趣的點,如邊緣上的點P(x,y)*/
for (int i = 0; i< number_of_features; i++)
{
/* 如果沒找到對應特徵點 */
if (optical_flow_found_feature[i] == 0)
continue;
int line_thickness;
line_thickness = 1;
/* CV_RGB(red, green, blue) is the red, green, and blue components
* of the color you want, each out of 255.
*/
CvScalar line_color;
line_color = CV_RGB(255, 0, 0);
/*畫箭頭,因為幀間的運動很小,所以需要縮放,不然看不見箭頭,縮放因子為3*/
CvPoint p, q;
p.x = (int)frame1_features[i].x;
p.y = (int)frame1_features[i].y;
q.x = (int)frame2_features[i].x;
q.y = (int)frame2_features[i].y;
double angle;
angle = atan2((double)p.y - q.y, (double)p.x - q.x);
double hypotenuse;
hypotenuse = sqrt(square(p.y - q.y) + square(p.x - q.x));
/*執行縮放*/
q.x = (int)(p.x - 5 * hypotenuse * cos(angle));
q.y = (int)(p.y - 5 * hypotenuse * sin(angle));
/*畫箭頭主線*/
/* "frame1"要在frame1上作畫.
* "p" 線的開始點.
* "q" 線的終止點.
* "CV_AA" 反鋸齒.
* "0" 沒有小數位.
*/
cvLine(frame1, p, q, line_color, line_thickness, CV_AA, 0);
/* 畫箭的頭部*/
p.x = (int)(q.x + 9 * cos(angle + pi / 4));
p.y = (int)(q.y + 9 * sin(angle + pi / 4));
cvLine(frame1, p, q, line_color, line_thickness, CV_AA, 0);
p.x = (int)(q.x + 9 * cos(angle - pi / 4));
p.y = (int)(q.y + 9 * sin(angle - pi / 4));
cvLine(frame1, p, q, line_color, line_thickness, CV_AA, 0);
}
/*顯示圖像*/
/*創建一個名為optical flow的窗口,大小自動改變*/
cvNamedWindow("Optical Flow", CV_WINDOW_NORMAL);
cvFlip(frame1, NULL, 2);
cvShowImage("Optical Flow", frame1);
/*延時,要不放不了*/
cvWaitKey(33);
/*寫入到文件中去*/
// cv::Mat m = cv::cvarrToMat(frame1);//轉換lpimgae到mat格式
// writer << m;//opencv3.0 version writer
}
cap.release();
cvWaitKey(33);
system("pause");
}
『拾』 如何利用opencv實現彩色圖像邊緣檢測演算法
在opencv中顯示邊緣檢測很簡單,只需調用一個cvCanny函數,其使用的是Canny演算法來實現對圖像的邊緣檢測.
函數原型為:
void cvCanny( const CvArr* image,CvArr* edges,double threshold1,double threshold2, int aperture_size=3 );
第一個參數為待檢測的圖像,注意一點,其必須是灰度圖.
第二個參數為輸出的邊緣圖,其也是一個灰度圖.
後三個參數與Canny演算法直接相關,threshold1和threshold2 當中的小閾值用來控制邊緣連接,大的閾值用來控制強邊緣的初始分割,aperture_size運算元內核大小,可以去看看Canny演算法.
從彩色圖到灰度圖需要使用到cvCvtColor函數,其接受三個參數,第一為輸入,第二為輸出,第三個為轉換的標識,我們這邊是RGB到GRAY,使用的是CV_RGB2GRAY.
參考demo代碼如下:
#include <iostream>
#include <string>
#include <sstream>
#include <opencv/cv.h>
#include <opencv/highgui.h>
using namespace std;
int String2int(const string& str_)
{
int _nre = 0;
stringstream _ss;
_ss << str_;
_ss >> _nre;
return _nre;
}
void DoCanny(const string& strFileName_)
{
//原彩色圖片
IplImage* _pIplImageIn = cvLoadImage(strFileName_.data());
if (_pIplImageIn == NULL)
{
return;
}
//彩色圖片轉換成灰度圖放置的圖片
IplImage* _pIplImageCanny = cvCreateImage(cvGetSize(_pIplImageIn), _pIplImageIn->depth, 1);
cvCvtColor(_pIplImageIn, _pIplImageCanny, CV_RGB2GRAY);//CV_RGB2GRAY將rgb圖轉成灰度圖
//只有邊緣路徑的圖片
IplImage* _pIplImageOut = cvCreateImage(cvGetSize(_pIplImageIn), IPL_DEPTH_8U, 1);
//邊緣檢測只能作用於灰度圖
if (_pIplImageCanny->nChannels != 1)
{
return;
}
//邊緣檢測操作
cvCanny(_pIplImageCanny, _pIplImageOut, 1, 110, 3);
cvNamedWindow("Src");
cvShowImage("Src", _pIplImageIn);
cvNamedWindow("Canny");
cvShowImage("Canny", _pIplImageOut);
cvWaitKey(0);
cvReleaseImage(&_pIplImageIn);
cvReleaseImage(&_pIplImageCanny);
cvReleaseImage(&_pIplImageOut);
cvDestroyWindow("Src");
cvDestroyWindow("Canny");
}
int main(int argc, char* argv[])
{
if (argc < 2)
{
cout << "You should give the filename of picture!" << endl;
return -1;
}
DoCanny(argv[1]);
return 0;
}