Opencv系列5_opencv命名方式读取指定路径DCM并批量另存为JPG图像

实例5:opencv命名方式读取指定路径DCM并批量另存为JPG图像

#include <iostream>
#include <io.h>  // 当中含有_finddata_t
#include <string>
#include <vector>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include <vtkSmartPointer.h>
#include <vtkImageViewer2.h>
#include <vtkImageCast.h>
#include <vtkDICOMImageReader.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkImageData.h>
#include <vtkCoordinate.h>

#define NUMBER 72//(CT文件数量为NUMBER-1)

using namespace std;
using namespace cv;

// 读入一个CT图,返回它的像素矩阵,使用OpenCV的Mat类型返回
// VTK读取DICOM图像并将像素值转给OpenCV的Mat对象
void dicomread(string inputFilename, Mat &img,vtkSmartPointer<vtkDICOMImageReader> &reader)
{
    img.create(512,512,CV_32SC1); 

	vtkSmartPointer<vtkImageCast> imageCast = 
		vtkSmartPointer<vtkImageCast>::New();

    reader->SetFileName(inputFilename.c_str());
	
    reader->Update();

	imageCast->SetInputConnection(reader->GetOutputPort());
	imageCast->SetOutputScalarTypeToInt();
	imageCast->Update();

	// 图像的基本信息
	int dims[3];
	reader->GetOutput()->GetDimensions(dims);
	
	//图像的像素值
	for(int k=0;k<dims[2];k++)
	{
	    for(int j=0;j<dims[1];j++)
		{
		    for(int i=0;i<dims[0];i++)
			{
				//转换数据类型,使用imagecast转到double(或float)
			    int* pixel = 
					(int*)(imageCast->GetOutput()->GetScalarPointer(i,j,k)); // 第i列第j行的像素值
				img.at<int>(j,i) = int(*pixel); // 第j行第i列的像素值
			}
		}
	}
}
//可视化DICOM图像
void showdicom(Mat I)
{
	double maxx=0,minn=0;
    double *max = &maxx;
	double *min = &minn;
	I.convertTo(I,CV_64FC1);
	minMaxIdx(I,min,max);
	for(int i=0;i<I.rows;i++)
	{
	    for(int j=0;j<I.cols;j++)
		{
		    I.at<double>(i,j) = 255*(I.at<double>(i,j)-minn)*1/(maxx-minn);
		}
	}
	
	minMaxIdx(I,min,max);
	for(int i=0;i<I.rows;i++)
	{
		for(int j=0;j<I.cols;j++)
			I.at<double>(i,j) = (I.at<double>(i,j)-minn)*1/(maxx-minn);
	}
	//cout << I <<endl;  //I为图像的像素值
	imshow("DICOM Image",I);
	waitKey(0);
}

int main()
{
	vector<Mat> images;
	char ad[128] = { 0 };
	for (int j = 1;j < NUMBER;j+=10) {
		string filename = format("F:\\Software\\VTK8.2.0\\vtk1\\helloVtk\\CT\\1066244_20110617_CT_102_215_%03d.dcm", j);
		Mat I1, G1;//I1为原图,G1为转化图
		vtkSmartPointer<vtkDICOMImageReader> reader =
			vtkSmartPointer<vtkDICOMImageReader>::New();
		// 读入dicom图,并将像素值传递给Mat对象I1
		dicomread(filename, I1, reader);
		flip(I1, I1, 0);
		//显示得到的Mat对象I1的信息*(单通道 大小512*512)
		cout << I1.channels() << "  " << I1.size() << endl;
		showdicom(I1);
		//保存图像
		sprintf_s(ad, "F:\\Software\\VTK8.2.0\\vtk1\\helloVtk\\JPG_images\\%d.jpg", j);
		imwrite(ad, I1);
	}
}

每按下空格,进行下一张图像读取和显示:

    

程序运行完后,在F:\Software\VTK8.2.0\vtk1\helloVtk\JPG_images路径下便可以查看另存的JPG图像:

本例程配套素材见源码整理文章下载(点击进入)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

亦我飞也

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值