OPENCV 笔记

//读视频
#include "stdafx.h"
#include <opencv2/opencv.hpp>
#include <opencv2\highgui\highgui.hpp>
using namespace std;
using namespace cv;
int _tmain(int argc, _TCHAR* argv[])
{
 VideoCapture cap;
 cap.open("Curve1.avi");
 if( !cap.isOpened() )
  return -1;
 namedWindow(winname,CV_WINDOW_AUTOSIZE);
 Mat frame;
 for(;;)
 {
  cap>>frame;
  imshow(“winname”, frame);
  char c=waitKey(33);
  if (c==27) {
   break;
  }
 }
 return 0;
}

//图像缩放
double fScale = 2.0;  //缩放倍数
Size dsize = Size(SrcImage.cols * fScale,SrcImage.rows * fScale);
Mat DstImage(dsize,SrcImage.type());
resize(SrcImage,DstImage,dsize);

//添加ROI
Mat logo;
Mat imageROI;
imageROI = SrcImage(cv::Rect(10,10,logo.cols,logo.rows));
logo.copyTo(imageROI);

//二值化
Mat dst; 
threshold(gray, dst, 0, 255, CV_THRESH_OTSU);//最大间内方差
threshold(gray, dst, 100, 255, CV_THRESH_BINARY);

// 获取最大值和最小值
Mat Hist;
double minVal = 0;
double maxVal = 0;
minMaxLoc(hist,&minVal, &maxVal, 0, 0);  //  cv中用的是cvGetMinMaxHistValue

//显示直方图的图像
int scale = 1;
int size = Hist.rows;
Mat imageShow(size * scale, size, CV_8U, Scalar(0));
int hpt = saturate_cast<int>(0.9 * size);
for(int i = 0; i < 270; i++)
{
    float value = hist.at<uchar>(i);           //   注意hist中是float类型    cv中用cvQueryHistValue_1D
    int realValue = saturate_cast<int>(value * hpt/maxVal);
    rectangle(imageShow,Point(size - 1, i*scale), Point(size - realValue, (i+1)*scale - 1), Scalar(255));
}
imshow("showImage", imageShow);

//计算时间
double t = (double)getTickCount();//开始时间
for (int i = 0; i <= 1000; i++){}//可替换成其他代码
t = (double)getTickCount() - t;//代码运行时间=结束时间-开始时间
printf("execution time = %gms\n", t*1000. / getTickFrequency());//转换时间单位并输出代码运行时间

//分解合并三基色
Mat mergeImage;
vector<Mat> channels; //定义一个Mat向量容器保存拆分后的数据
split(srcImage,channels); //通道的拆分
imageBlue = channels.at(0); //提取蓝色通道的数据
imageGreen = channels.at(1);//提取绿色通道的数据
imageRed = channels.at(2);//提取红色通道的数据
merge(channels,mergeImage); //对拆分的通道数据合并

//直方图匹配
void cvMatchHist( Mat &src, Mat &dst)
{
 int src_hist[256] = {0};
 for (int i = 0; i < src.rows; i++)
 {
  for (int j = 0; j < src.cols; j++)
  {
   int k = src.at<uchar>(i, j);
   src_hist[k]++;
  }
 }
 int lre = 0;
 for (int i = 0; i < 256; i++)
 {
  lre += src_hist[i];
  src_hist[i] = lre;
 }
 int dst_hist[256] = {0};
 for (int i = 0; i < src.rows; i++)
 {
  for (int j = 0; j < src.cols; j++)
  {
   int k = dst.at<uchar>(i, j);
   dst_hist[k]++;
  }
 }
 lre = 0;
 for (int i = 0; i < 256; i++)
 {
  lre += dst_hist[i];
  dst_hist[i] = lre;
 }
 for (int i = 0; i < 256; i++)
 {
  int abs_min = 1000;
  for (int j = 0; j < 256; j++)
  {
   if (abs(src_hist[i] - dst_hist[i]) < abs_min)
   {
    abs_min = abs(src_hist[i] - dst_hist[i]);
    src_hist[i] = dst_hist[i];
   }
  }
 }
 for (int i = 0; i < src.rows; i++)
 {
  for (int j = 0; j < src.cols; j++)
  {
   int k = src.at<uchar>(i, j);
   src.at<uchar>(i, j) = uchar(src_hist[k] * 255 / src_hist[255]);
  }
 }
}

 //opnecv3 lib

opencv_ts300d.lib
opencv_world300d.lib

//-- Check its extreme values
cv::minMaxLoc( disp_l, &minVal, &maxVal );
cout<<"Min disp: Max value"<< minVal<<maxVal; //numberOfDisparities.= (maxVal - minVal)

//-- Display it as a CV_8UC1 image
disp_l.convertTo(disp8u_l, CV_8U, 255/(maxVal - minVal));//(numberOfDisparities*16.)

cv::minMaxLoc( disp_r, &minVal, &maxVal );
cout<<"Min disp: Max value"<< minVal<<maxVal; //numberOfDisparities.= (maxVal - minVal)

//-- Display it as a CV_8UC1 image
disp_r.convertTo(disp8u_r, CV_8U, 255/(maxVal - minVal));//(numberOfDisparities*16.)

cv::normalize(disp8u_l, disp8u_l, 0, 255, CV_MINMAX, CV_8UC1); // obtain normalized image
cv::normalize(disp8u_r, disp8u_r, 0, 255, CV_MINMAX, CV_8UC1); // obtain normalized image

//Cmake opencv

set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/CMakeScripts)
set(OpenCV_DIR D:/Program/opencv2.4.9/build)

 

//输入图像
//输出图像
//输入图像颜色通道数
//x方向阶数
//y方向阶数
Sobel(src,dst,src.depth(),1,1);
imwrite("sobel.jpg",dst);

//输入图像
//输出图像
//输入图像颜色通道数
Laplacian(src,dst,src.depth());
imwrite("laplacian.jpg",dst);

//输入图像
//输出图像
//彩色转灰度
cvtColor(src,src,CV_BGR2GRAY); //canny只处理灰度图

//输入图像
//输出图像
//低阈值
//高阈值,opencv建议是低阈值的3倍
//内部sobel滤波器大小
Canny(src,dst,50,150,3);
imwrite("canny.jpg",dst);

//截取mat

Mat left_i = left.rowRange(row0, row1);
Mat right_i = right.colRange(row0, row1);

// Iplimage转mat

cvarrToMat

//Mat 保存为txt

string labelname("disp.txt");
ofstream file(labelname);
if (!file.is_open())
{
return 0;
}

for (int i = 0; i < disp.rows; i++)
{
for (int j = 0; j < disp.cols; j++)
{
file << disp.at<short>(i,j) << ",";
}
file << "\n";
}
file.close();

posted @ 2016-08-30 15:47  wl_v_2016  阅读(471)  评论(0编辑  收藏  举报