天天看點

使用opencv建立一張純黑色的圖檔與其他圖檔進行合成

功能:使用opencv建立一張純色的的照片,顔色以及大小尺寸自己設定。

說明:這裡的cvSize控制建立圖檔的大小,就是長和寬。那麼在使用for循環的時候注意i  j的範圍。使用這種方法你可以建立任意大小和自己喜歡的顔色的圖檔做圖檔的底片用來和要被顯示的圖檔進行和,把純色當底片,根據顯示器比例來設定你建立的圖檔比例。那麼在全屏顯示沒和成之前的圖檔的時候圖檔縮放是根據顯示器的比例進行縮放的,是以造成顯示器兩邊或者上邊有白色的部分,那麼當把黑色的圖檔當底片與要顯示的圖檔進行合成,那麼在全屏顯示的時候不是圖檔的部分就是黑色的。

建立一張黑色的圖檔

#include<iostream>
//#include"highgui.h"
//#include"cv.h"
#include "opencv2/opencv.hpp"

using namespace std;
using namespace cv;

int main()
{
  IplImage* img1 = cvCreateImage(cvSize(320, 180), IPL_DEPTH_8U, 3);
  //uchar r1, g1, b1;
  for (int i = 0; i < img1->height; i++)
  {
    uchar *ptrImage = (uchar*)(img1->imageData + i * img1->widthStep);
    //uchar *ptrDst = (uchar*)(img->imageData + i * img->widthStep);

    for (int j = 0; j < img1->width; j++)
    {
      //b1 = ptrImage[3 * j + 0];
      //g1 = ptrImage[3 * j + 1];
      //r1 = ptrImage[3 * j + 2];

      //ptrDst[3 * (j + 400) + 0] = 0;
      //ptrDst[3 * (j + 400) + 1] = 0;
      //ptrDst[3 * (j + 400) + 2] = 0;
      ptrImage[3 * j + 0]=0;
      ptrImage[3 * j + 1]=0;
      ptrImage[3 * j + 2]=0;
    }
  }
  cvSaveImage("c://chenxun.jpg", img1);
}      
使用opencv建立一張純黑色的圖檔與其他圖檔進行合成

代碼如下:

</pre><pre name="code" class="cpp">//----------------------------------------------------
//author:chen(stallman)
//time:2015.1.10
//----------------------------------------------------

#include<iostream>
#include<cv.h>
#include<highgui.h>

using namespace std;
using namespace cv;

//合成圖檔
int main()
{
  clock_t start, finish;
  start = clock();
  double   duration;
  IplImage*img1 = cvLoadImage("1.jpg");
  //IplImage* img2 = cvLoadImage("2.jpg");
  IplImage*img = cvLoadImage("3200-1800.jpg", 1);

  //IplImage* img = cvCreateImage(cvSize(img1->width + img2->width, img1->height), img1->depth, 3);

  //cout << img1->widthStep << endl;
  //cout << img2->widthStep << endl;

  uchar r1, g1, b1;
  for (int i = 0; i < img1->height; i++)
  {
    uchar *ptrImage = (uchar*)(img1->imageData + i * img1->widthStep);
    uchar *ptrDst = (uchar*)(img->imageData + i * img->widthStep);

    for (int j = 0; j < img1->width; j++)
    {
      b1 = ptrImage[3 * j + 0];
      g1 = ptrImage[3 * j + 1];
      r1 = ptrImage[3 * j + 2];

      ptrDst[3 * (j+400) + 0] = b1;
      ptrDst[3 * (j+400) + 1] = g1;
      ptrDst[3 * (j+400) + 2] = r1;
    }
  }

  cvSaveImage("result.jpg", img);
  finish = clock();
  duration = (double)(finish - start) / CLOCKS_PER_SEC;
  cout << duration << endl;

  cvNamedWindow("img.jpg", 0);
  cvShowImage("img.jpg", img);
  waitKey();
  return 0;
}