📄 image.cpp
字号:
BYTE val = Pixel(rx, ry); return val;}double CByteImage::InterpolateLinear(double x, double y) const{ int left = (int) floor(x); if (left<-1 || m_width<=left) { return 0; } int top = (int) floor(y); if (top<-1 || m_height<=top) { return 0; } int right = left+1; int bottom = top+1; BYTE val_lt, val_lb, val_rt, val_rb; if (left==-1) { val_lt = 0; val_lb = 0; } else { if (top==-1) { val_lt = 0; } else { val_lt = Pixel(left, top); } if (bottom==m_height) { val_lb = 0; } else { val_lb = Pixel(left, bottom); } } if (right==m_width) { val_rt = 0; val_rb = 0; } else { if (top==-1) { val_rt = 0; } else { val_rt = Pixel(right, top); } if (bottom==m_height) { val_rb = 0; } else { val_rb = Pixel(right, bottom); } } double frac_x = x-(double)left; double frac_y = y-(double)top; double val = (1.0-frac_y)*((1.0-frac_x)*val_lt+frac_x*val_rt) + frac_y*((1.0-frac_x)*val_lb+frac_x*val_rb); return val;}void CByteImage::Rotate(double degrees, int xCenter, int yCenter){ if (degrees==0.0) return; //#ifdef DEBUG if (xCenter<0 || xCenter>=m_width || yCenter<0 || yCenter>=m_height) { char* buf = (char*) alloca(512*sizeof(char)); sprintf(buf, "%f %d %d %d %d\n", degrees, xCenter, yCenter, m_width, m_height); throw ITException(buf); } //#endif // DEBUG ASSERT(0<=xCenter && xCenter<m_width); ASSERT(0<=yCenter && yCenter<m_height); CByteImage old_img; old_img.Copy(*this); double rad = degrees*M_PI/180.0; double cos_rad = cos(rad); double sin_rad = sin(rad); for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { double rel_x = x-xCenter; double rel_y = y-yCenter; double rot_x = rel_x*cos_rad - rel_y*sin_rad; double rot_y = rel_x*sin_rad + rel_y*cos_rad; double val = old_img.InterpolateLinear(xCenter+rot_x, yCenter+rot_y); ASSERT(0.0<=val && val<=255.0); Pixel(x, y) = (BYTE) val; } } }void CByteImage::Crop(const CRect& area){ ASSERT(area.left<area.right && area.top<area.bottom); ASSERT(0<=area.left && area.right<m_width); ASSERT(0<=area.top && area.bottom<m_height); int old_width = m_width; BYTE* pOldData = m_pData; if (!m_data_is_local) { Allocate(area.right-area.left, area.bottom-area.top); // previous call changed m_width and m_height also } else { m_width = area.right-area.left; m_height = area.bottom-area.top; } for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { Pixel(x, y) = pOldData[(area.left+x) + (area.top+y)*old_width]; } } m_data.resize(m_width*m_height);}/* this is not very efficient and should only be used for testing*/void CByteImage::Standardize(int ip_method){ if (ip_method==0) { // equalize histogram ASSERT(0); /* IplImage* ipl_img = CreateIplImage(); const int range = 256; IplLUT lut = { range+1, NULL,NULL,NULL, IPL_LUT_LOOKUP }; IplLUT* plut = &lut; lut.key = new int[range+1]; lut.value = new int[range]; // Initialize the histogram levels for(int i=0; i<=range; i++) lut.key[i] = i; // Compute histogram iplComputeHisto( ipl_img, &plut ); // Equalize histogram = rescale range of image data iplHistoEqualize( ipl_img, ipl_img, &plut ); delete[] lut.key; delete[] lut.value; DeleteIplImage(ipl_img); */ } else if (ip_method==1) { // normalize image // find avg int sum=0; { for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { sum += Pixel(x, y); } } } double N = m_height*m_width; double avg = (double)sum/N; // find stddev double varsum=0; { for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { double val = (double)Pixel(x, y)-avg; varsum += val*val; } } } double stddev = sqrt(varsum/N); // normalize { for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { double newpix = ((double)Pixel(x, y)-avg) / stddev; newpix *= 127.5; newpix += 127.5; if (newpix<0) { Pixel(x, y) = 0; } else if (newpix>255) { Pixel(x, y) = 255; } else { Pixel(x, y) = (BYTE) newpix; } } } } } else if (ip_method==2) { // adjust image range BYTE min=255; BYTE max=0; { for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { BYTE pix = Pixel(x, y); if (max<pix) max=pix; if (min>pix) min=pix; } } } double fac = 255.0/(double)(max-min); // move pixel values { for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { Pixel(x, y) = (BYTE) ((double)(Pixel(x, y)-min)*fac); } } } } else { ASSERT(0); }}// load image from diskvoid CByteImage::ImportFromFile(string filename) throw (ITEFile){ if (filename.compare("")==0) { throw ITEFile("-", "no filename given"); }#ifdef WIN32 filename = ConvertPathToWindows(filename);#endif // WIN32#if defined(IMG_LIB_MAGICK) try { Image tmpimg; tmpimg.read(filename.c_str()); int width = tmpimg.baseColumns(); int height = tmpimg.baseRows(); const PixelPacket* pData = tmpimg.getConstPixels(0, 0, width, height); ASSERT(pData); Allocate(width, height); for (int y=0; y<height; y++) { for (int x=0; x<width; x++) { Pixel(x, y) = (BYTE) (0.212671 * (double)(pData->red>>(QuantumDepth-8)) + 0.715160 * (double)(pData->green>>(QuantumDepth-8)) + 0.072169 * (double)(pData->blue>>(QuantumDepth-8))); pData++; } } } catch (Magick::Exception &error) { throw ITEFile(filename, string("exception during image reading with Magick:\n") +error.what()); } #elif defined(IMG_LIB_OPENCV) // load image, don't change number of channels IplImage* img = cvLoadImage(filename.c_str(), -1); if (img==NULL) { throw ITEFile(filename, "load failed in OpenCV"); } Allocate(img->width, img->height); if (img->nChannels==3) { CvSize size = cvSize(img->width, img->height); IplImage* gray = cvCreateImage(size, IPL_DEPTH_8U, 1); cvCvtColor(img, gray, CV_BGR2GRAY); cvReleaseImage(&img); img = gray; } if (img->nChannels==1) { ASSERT(m_pData); memcpy(img->imageData, m_pData, m_width*m_height*sizeof(BYTE)); } else { throw ITEFile(filename, "can not import"); } cvReleaseImage(&img);#elif defined(IMG_LIB_NONE) throw ITEFile(filename, "file import functionality not available");#else#error IMG_LIB not defined - you must at least define IMG_LIB_NONE explicitely#endif // IMG_LIB}void CByteImage::ExportToFile(string filename) throw (ITEFile){ if (filename.compare("")==0) { throw ITEFile("-", "no filename given"); }#ifdef WIN32 filename = ConvertPathToWindows(filename);#endif // WIN32#if defined(IMG_LIB_MAGICK) Image tmpimg(Geometry(m_width, m_height), Color("red")); tmpimg.modifyImage(); PixelPacket* pTmpPixel = tmpimg.getPixels(0, 0, m_width, m_height); for (int y=0; y<m_height; y++) { for (int x=0; x<m_width; x++) { BYTE p = Pixel(x, y); pTmpPixel->red = p; pTmpPixel->green = p; pTmpPixel->blue = p; pTmpPixel++; } } tmpimg.type(GrayscaleType); tmpimg.syncPixels(); try { tmpimg.write(filename.c_str()); } catch (Exception &error) { throw ITEFile(filename, string("exception in Magick.read: ")+error.what()); }#else // IMG_LIB throw ITEFile(filename, "file export functionality not available");#endif // IMG_LIB}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -