Opencv系列4_opencv读取指定路径DCM并批量另存为JPG图像

该程序使用OpenCV和VTK库实现了从DICOM格式的医学图像中读取指定路径下的CT数据,并将这些图像批量转换为JPG格式。通过VTK的vtkDICOMImageReader读取DCM文件,然后使用OpenCV进行图像处理和显示,最后将处理后的图像保存到指定的JPG目录。程序展示了如何在C++中结合这两个库进行图像处理。
摘要由CSDN通过智能技术生成

 实例4: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>

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()
{
	//要读取的CT数据文件名
	string vc[8] = 
	{ 
		"1066244_20110617_CT_102_215_001.dcm" ,
		"1066244_20110617_CT_102_215_011.dcm" ,
		"1066244_20110617_CT_102_215_021.dcm" ,
		"1066244_20110617_CT_102_215_031.dcm" ,
		"1066244_20110617_CT_102_215_041.dcm" ,
		"1066244_20110617_CT_102_215_051.dcm" ,
		"1066244_20110617_CT_102_215_061.dcm" ,
		"1066244_20110617_CT_102_215_071.dcm" ,
	};
	int i = 0;
	char ad[128] = { 0 };
	for (int j = 0;j < 8;j++) {
		//指定要读取的CT数据文件位置
		string filename = "F:\\Software\\VTK8.2.0\\vtk1\\helloVtk\\CT\\" +   vc[i];//quarter  CT000895  1066244_20110617_CT_102_215_001
		i++;
		Mat I1;//I1为原图,G1为转化图
		vtkSmartPointer<vtkDICOMImageReader> reader =
			vtkSmartPointer<vtkDICOMImageReader>::New();
		// 读入dicom图
		dicomread(filename, I1, reader);
		//翻转图像
		flip(I1, I1, 0);
		//显示得到的Mat对象I1的信息*(单通道 大小512*512)
		cout << I1.channels() << "  " << I1.size() << endl;
		showdicom(I1);
		//指定路径对图像进行批量另存为JPG格式图像
		//给的另存路径和文件格式
		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、付费专栏及课程。

余额充值