1.读取文件夹连续图片
刚开始学习图像处理,开始只能一次读入一张世睁图片,今天从网上查了一些资料,自己写了一个可以连续读取多张图片函数。
char filename[100]
char windowname[100]
IplImage* pScr
unsigned char *Readfigsmethod1(int num)// 读入num个图片
{
for(int i=1i<=numi++)
{
sprintf(filename,"D:/test/%d.jpg",i)// 将图片以数字命名:例如1.jpg 2.jpg等,放入D:/test/文件夹下
sprintf(windowname,"window%d.jpg",i)
pScr=cvLoadImage(filename,1)//导入图片
cvNamedWindow(windowname,CV_WINDOW_AUTOSIZE)
cvShowImage(windowname,pScr)//显示图片
//cvWaitKey(0)
}
cvWaitKey(0)
cvReleaseImage(&pScr)//释放图片
cvDestroyAllWindows()//旅返哪销毁窗口拆码
return 0
}
注释:连续读取图片主要问题在于filename指向图片目录, sprintf(filename,"D:/test/%d.jpg",i)的使用可以使得filename可以从1.jpg,2.jpg,一直到num.jpg. filename=D:/test/i.jpg.
2.RGB分量显示,图像灰度化
// RGBSPLIT.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
//#include "afx.h"
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char filename[100]
char filename1[100]
char windowname[100]
IplImage* pScr
IplImage *img1
IplImage *img=0
IplImage *img_red=0
IplImage *img_green=0
IplImage *img_blue=0
unsigned char *Readfigsmethod1(int num)//实现连续读取图片的函数
unsigned char *RGBsplit(IplImage *img)//实现RGB量分开显示的函数
unsigned char *RGBtoGray(IplImage *img)//实现灰度化函数
int main(int argc, char *argv[])
{
img=cvLoadImage("D:\\test\\1.jpg")
cvNamedWindow("lena",CV_WINDOW_AUTOSIZE)//创建窗口,窗口名字lena
cvShowImage("lena",img)//载入转化后的图像
RGBsplit(img)//调用RGB分开显示函数,若想实现其他功能,在此处调用其他函数即可。但是显示后cvReleaseImage()中相应参数要更改成显示的对象。
cvWaitKey(0)
cvReleaseImage(&img)
cvReleaseImage(&img_red)
cvDestroyAllWindows()
return 0
}
unsigned char *Readfigsmethod1(int num)
{
for(int i=1i<=numi++)
{
sprintf(filename,"D:/test/%d.jpg",i)
sprintf(windowname,"window%d.jpg",i)
pScr=cvLoadImage(filename,1)
cvNamedWindow(windowname,CV_WINDOW_AUTOSIZE)
cvShowImage(windowname,pScr)
//cvWaitKey(0)
}
cvWaitKey(0)
cvReleaseImage(&pScr)
cvDestroyAllWindows()
return 0
}
unsigned char *RGBtoGray(IplImage *img)
{
img1 = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1)
//色彩空间转换,将源彩色图像img转化成目标灰色图像imag1
cvCvtColor(img,img1,CV_BGR2GRAY)//关键
cvNamedWindow("GrayImage",CV_WINDOW_AUTOSIZE)//创建窗口,窗口名字GrayImage
cvShowImage("GrayImage",img1)//载入转化后的图像
return 0
}
unsigned char *RGBsplit(IplImage *img)
{
//IplImage* imgeR,img_blue,imageG,imgGRAY
int width=img->width
int height=img->height
int channel=img->nChannels
int widthStep=img->widthStep
int i,j
img_red= cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3)
img_green= cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3)
img_blue= cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,3)
for (i=0i<heighti++)
{
for (j=0j<widthj++)
{
CvScalar t=cvGet2D(img,i,j)
double s0=t.val[0]
double s1=t.val[1]
double s2=t.val[2]
CvScalar m_blue=cvScalar(s0,0,0,0)
CvScalar m_green=cvScalar(0,s1,0,0)
CvScalar m_red=cvScalar(0,0,s2,0)
cvSet2D(img_blue,i,j,m_blue)
cvSet2D(img_green,i,j,m_green)
cvSet2D(img_red,i,j,m_red)
}
}
cvNamedWindow("imgred",1)
cvShowImage("imgred",img_red)
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)