vc 下 用 opencv 實現的— 遍曆映像,傳回值為 a 的 像素點 的座標

來源:互聯網
上載者:User

小程式,隨便記錄下下~~

遍曆一幅映像,返回 像素值 為 0 的 點的座標:

#include "stdafx.h"#include <stdio.h>#include <cv.h>#include <highgui.h>#include "cvaux.h" //必須引此標頭檔using namespace std;vector<CvPoint> cal_dark(IplImage *img );int main( int argc, char** argv ){ IplImage *img = cvLoadImage("rice.png",0);if(img == NULL){printf("img load failed!\n");return 0;}cvThreshold(img, img, 128, 255, CV_THRESH_BINARY);vector<CvPoint> myVector = cal_dark(img);int size = myVector.size();int i;for(i = 0; i < size; i++){//printf("[%d, %d]", myVector[i].x, myVector[i].y);if( i == 10)printf("\n");}///////////////////////////test////////////////////////IplImage *test = cvCreateImage(cvGetSize(img), 8, 1);memset(test->imageData, 255, test->widthStep * test->height);for(i = 0; i < size; i++){CvScalar s;s.val[0] = 0.0;cvSet2D(test, myVector[i].x, myVector[i].y, s);}cvNamedWindow("img"); //原圖二值化後cvNamedWindow("test"); //根據得到的座標產生的映像cvShowImage("img",img);cvShowImage("test",test);cvWaitKey(-1);cvReleaseImage(&img);cvReleaseImage(&test);cvDestroyAllWindows();getchar();return 0;}vector<CvPoint> cal_dark(IplImage *img ){int w = img->width;int h = img->height;//CvPoint *pos_dark = (CvPoint *)malloc(w * h *sizeof(CvPoint));vector<CvPoint> PointVector;int i, j;for(i = 0; i < h; i++){for(j = 0; j < w; j++){CvScalar s = cvGet2D(img, i, j);if(s.val[0] == 0.0){CvPoint point=cvPoint(i, j);PointVector.push_back(point);}}}return PointVector;}

結果:

===================================================================================

PS:

建立一幅映像,用memset置為 255 時:

IplImage *test = cvCreateImage(cvGetSize(img), 8, 1);
memset(test->imageData, 255, test->widthStep * test->height);

注意, 是把  IplImage 映像的 資料 部分 置為 255 ,資料起始地址是  
 test->imageData


話說, IplImage 的頭 長度 怎麼計算來著?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.