映像與矩陣的操作

來源:互聯網
上載者:User

#include"stdafx.h"

#include<cv.h>

#include<cxcore.h>

#include<highgui.h>       
//程式所需的標頭檔

 

//兩個自訂的函數,用於實現映像的條件複製,效果見如下附圖。

IplImage*myCopyImage(IplImage* img, int Row, int Col, int Width, int Height);

CvMat*myCopyMat(IplImage* img, int Row, int Col, int Width, int Height);

 

/*主函數,它會調用本常式中的兩個函數進行映像的剪下操作*/

int _tmain(int argc, _TCHAR*argv[])

{

    //Step1:開啟並顯示原始映像,效果見圖2啦~~    

    IplImage *img = cvLoadImage("Koala.jpg", -1);      
//讀取映像Koala.jpg

    cvNamedWindow("Image:",1);                      
//建立一個映像視窗

    cvShowImage("Image:",img);                      
//顯示讀取的映像

   

    //Step2:運用內建函式複製並顯示複製後的映像,效果和圖2一樣,就是多了個視窗顯示這幅映像,為了對比一下而已,所以就不費篇幅啦~~

    IplImage *copy = cvCloneImage(img);            
//調用內建函式複製映像

    cvNamedWindow("Copy",1);                           
//建立一個映像視窗

    cvShowImage("Copy",copy);                           
//顯示複製的映像

 

    //Step3:自己編寫複製映像塊函數,並顯示複製的映像塊(映像方式),剪下了起始位置(200,200),寬高(300,300)的映像,函數定義看最上面,實現看後面,效果見圖3

    IplImage *Block = myCopyImage(img,200,200,300,300);//自訂條件複製函數複製映像

    if(Block)                                      
//判斷是否成功複製

    {

        cvNamedWindow("SubImage",1);                   
//建立一個映像視窗

        cvShowImage("SubImage",Block);             
//顯示複製的映像塊

        cvWaitKey();                                   
//等待鍵盤事件

        cvDestroyWindow("SubImage");                   
//關閉映像視窗

        cvReleaseImage(&Block);                    
//清除映像塊

    }

   

    //Step4:自己編寫複製映像塊函數,並顯示複製的映像塊(矩陣方式),和前面的效果是一樣的,但是是對矩陣進行操作,顯示的也是矩陣,效果還是圖3的樣子~~

    CvMat *a = myCopyMat(img,200,200,300,300);     
//自訂條件複製函數複製映像

    if(a)                                          
//判斷是否成功複製

    {

        cvNamedWindow("Matrix",1);                 
//建立一個映像視窗

        cvShowImage("Matrix",a);                       
//顯示複製的映像塊

        cvWaitKey();                                   
//等待鍵盤事件

        cvDestroyWindow("Matrix");                 
//關閉映像視窗

        cvReleaseMat(&a);                          
//清除映像塊

    }

    cvDestroyWindow("Image:");                     
//關閉原映像視窗

    cvReleaseImage(&img);                          
//清除原映像

    cvReleaseImage(&copy);                          
//清除內建函式複製的映像

    return 0;

}

 

/* 上面就是調用這個函數進行剪下的,通過輸入起始位置和映像的寬高就可以剪下你想要的部分,多通道單通道都適用哦~~*/

IplImage*myCopyImage(IplImage* img, int Row, int Col, int Width, int Height)

{

    int img_Width = img->width;

    int img_Height = img->height;

    int img_Channel = img->nChannels;

    printf("源映像參數:\n\t寬:%d\n\t高:%d\n\t通道數:%d\n",img_Width, img_Height, img_Channel);                     
//擷取原映像的基本參數

 

    if((Row<0) | (Col<0) | ((Row+Height)>img_Width) |((Col+Width)>img_Height))

    {

        printf("\nError:\n\t請輸入正確的起始位置和複製寬高!\n\t返回為空白");

        return NULL;

    }                               //對條件進行判斷,防止錯誤複製

   

    IplImage* Block =cvCreateImage(cvSize(Width,Height),IPL_DEPTH_8U,img_Channel);

                                    //建立一幅空映像

    int b_step = Block->widthStep;

   

    for (int i=0;i<Height;i++)

        for(int j=0;j<Width;j++)

            for(int k=0;k<img_Channel;k++)

            {

Block->imageData[i*b_step+j*img_Channel+k] =

                    img->imageData[(i+Row)*

                    (img->widthStep)+(j+Col)*img_Channel+k];

}                   //三次迴圈,將原圖中所需複製的每一個像素點填充到新的映像中去

    return Block;

}

 

/* 用矩陣的方式複製映像的部分或者全部,效果和上面一樣,過程起始頁差不多,只是裡面的操作對象就是矩陣。PS:當然映像也是矩陣,要是這麼看的話就無差別了~~*/

CvMat *myCopyMat(IplImage*img, int Row, int Col, int Width, int Height)

{

    int img_Width = img->width;

    int img_Height = img->height;

    int img_Channel = img->nChannels;

    printf("源映像參數:\n\t寬:%d\n\t高:%d\n\t通道數:%d\n",img_Width, img_Height, img_Channel);                     
//擷取原映像的基本參數

 

    if((Row<0) | (Col<0) | ((Row+Height)>img_Width) |((Col+Width)>img_Height))

    {

        printf("\nError:\n\t請輸入正確的起始位置和複製寬高!\n\t返回為空白");

        return NULL;

    }                               //對條件進行判斷,防止錯誤複製

   

    CvMat *a = cvCreateMat(Height, Width,CV_MAKETYPE(CV_8U,img_Channel));

                                    //建立一幅空矩陣

    int b_step = a->step;

    for (int i=0;i<Height;i++)

        for(int j=0;j<Width;j++)

            for(int k=0;k<img_Channel;k++)

            {

                a->data.ptr[i*b_step+j*img_Channel+k] =img->imageData[(i+Row)*

                    (img->widthStep)+(j+Col)*img_Channel+k];

            }

    return a;

}


 

附圖

圖1源映像(Koala.jpg)的簡單參數

圖2.源映像

   

圖3.操作後的映像

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.