主函数
#include "image.h"
#include <string>
#include<cmath>
#include<math.h>
int main(int argc, char *argv[])
{
Image *Im = new Image();//定义的一个读写图片的类,用的tif库
std::string outputpath;
std::string inputpath;
std::cout << "input image data input path:";
std::cin >> inputpath;
std::cout << "input image data outnput path:";
std::cin >> outputpath;
QString name_a = ".tif";
QDir dir(QString::fromStdString(inputpath));
if (!dir.exists())
{
qWarning("cannot open dir");
}
QStringList filter;
QString fit("*");
fit.append(name_a);
filter << fit;
dir.setNameFilters(filter);
QStringList abclist = dir.entryList();
QString str_temp;
QString str_temp1;
QStringList file_input_list;
QStringList file_output_list;
// std::cout << abclist.size();
int thenumber = abclist.size();
for (int kk = 0; kk < abclist.size(); kk++)
{
str_temp = QString::fromStdString(inputpath);
str_temp.append("/");
str_temp.append(abclist.at(kk));
file_input_list.append(str_temp);
str_temp.clear();
str_temp1 = QString::fromStdString(outputpath);
str_temp1.append("/");
str_temp1.append(abclist.at(kk));
file_output_list.append(str_temp1);
str_temp1.clear();
}
int numProcs = omp_get_num_procs();
int size = file_input_list.length();
int width = Im->get_Image_width(file_input_list[0].toStdString().c_str());//获取图片的宽
int height = Im->get_Image_height(file_input_list[0].toStdString().c_str());//获取图片的高
#pragma omp parallel for num_threads(numProcs)
for (int num = 0; num < size; num++)
{
uint16 *nImage = new uint16[width*height];
memset(nImage, 0, sizeof(uint16)*width*height);
Im->Read16BitImage(file_input_list[num].toStdString().c_str(), nImage, width, height);
unsigned char * nImage8 = new unsigned char[width*height];
memset(nImage8, 0, sizeof(unsigned char)*width*height);
Im->transform16_8bit(nImage, nImage8,width, height);
/*Im->connect_filter(nImage, width, height, depth);*/
/*Im->Median_filter(nImage, width, height, depth);*/
std::cout << num << std::endl;
Im->Write8BitImage(file_output_list[num].toStdString().c_str(), nImage8, width, height);
delete[]nImage;
nImage = NULL;
delete[]nImage8;
nImage8 = NULL;
}
return 1;
主函数用到的函数
int Image::get_Image_width(const char path[])
{
TIFFSetWarningHandler(0);
TIFF *tif = TIFFOpen(path, "r");
if (tif == 0) {
printf(" open TIFF file %s failed\n", path);
return false;
}
int width = 0;
uint16 comperID;
uint32 perrow;
TIFFGetField(tif, TIFFTAG_COMPRESSION, &comperID);
TIFFGetField(tif, TIFFTAG_ROWSPERSTRIP, &perrow);
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFClose(tif);
std::cout << "TIFFTAG_COMPRESSION is " << comperID << std::endl;
std::cout << "TIFFTAG_ROWSPERSTRIP is " << perrow << std::endl;
return width;
}
int Image::get_Image_height(const char path[])
{
TIFFSetWarningHandler(0);
TIFF *tif = TIFFOpen(path, "r");
if (tif == 0) {
printf(" open TIFF file %s failed\n", path);
return false;
}
int height = 0;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
TIFFClose(tif);
return height;
}
bool Image::Read16BitImage(const char path[], uint16* &Image, int &width, int &height)
{
TIFFSetWarningHandler(0);
TIFF *tif = TIFFOpen(path, "r");
if (tif == 0) {
printf(" open TIFF file %s failed\n", path);
return false;
}
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
//Image = new uint16[width*height];
TIFFSetWarningHandler(0);
/*TIFF *tif = TIFFOpen(path, "r");*/
if (tif == 0) {
printf(" open TIFF file %s failed\n", path);
}
else
{
uint32 row;
for (row = 0; row <(uint32)height; row++)
{
//需要注意,这里内存偏移必须写为(row-ys)*width
TIFFReadScanline(tif, (Image + row*width), row);
}
TIFFClose(tif);
}
return true;
}
void Image::transform16_8bit(uint16 *image, unsigned char* data, int width, int height)
{
int maxdata = 0;
int mindata = 65535;
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
if (image[j + i*width]>maxdata)
maxdata = image[j + i*width];
if (image[j + i*width] < mindata&&image[j + i*width] != 0)
mindata = image[j + i*width];
}
//unsigned char* data = new unsigned char[width*height];
//memset(data, 0, sizeof(unsigned char)*width*height);
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
if (image[j + i*width] != 0)
{
data[j + i*width] = 254.0 / (double)(maxdata - mindata)*(image[j + i*width] - mindata) + 1;
}
}
}
bool Image::Write8BitImage(const char path[], uint8* Image, int width, int height)
{
TIFF *tif = TIFFOpen(path, "w");
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1); //每个像素点应有几个通道
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8); //每个通道的位宽,这里为8位图片
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_LEFTTOP);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK); //设定图片显示时,灰度值越小的像素越接近黑色
// TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, TIFFDefaultStripSize(tif, width));
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, 1);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
uint8 * curRow = new uint8[width];
for (uint32 row = 0; row < height; row++)
{
//memcpy(curRow, (buffer+row*width), width); // check the index here, and figure out why not using h*linebytes
if (TIFFWriteScanline(tif, Image + row*width, row, 0) < 0)
break;
}
delete[]curRow;
TIFFClose(tif);
return true;
}