Opencv系列1_opencv对单张DCM文件的读取并显示

 实例1:opencv对单张DCM文件的读取并显示

#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 filename = "1066244_20110617_CT_102_215_001.dcm" ;
		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);
}

要读取的dcm文件放在工程目录下:

可见读取的为单通道的512*512像素大小的图像:

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

亦我飞也

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

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

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

打赏作者

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

抵扣说明:

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

余额充值