// Keep only these band numbers GeoImage GeoImage::select(vector<int> nums) { GeoImage imgout(*this); vector<GeoRaster> _bands; vector<string> _names; vector<int> _bandnums; // TODO - for fun, replace with lambda function and map for (vector<int>::const_iterator i=nums.begin(); i!=nums.end(); i++) { _bands.push_back(_RasterBands[*i-1]); _names.push_back(_BandNames[*i-1]); } imgout._RasterBands = _bands; imgout._BandNames = _names; return imgout; }
Mat ImageTransforms::QImage2Mat_ROI(QImage *image, Rectangle<int> *region) { int w = image->width(), h = image->height(); Mat imgout(h,w,CV_8UC1); return imgout; // QImage *f = m_data->fgImage; // uchar *pixels = f->bits(); // uchar *color_pixels = image.bits(); // int color_bpl = image.bytesPerLine(); // int x, y, x0 = rect.xleft, y0 = rect.ytop, w = f->width(), // x1 = std::min(w - 1, x0 + rect.width), h=f->height(), // y1 = std::min(f->height() - 1, y0 + rect.height), // bpl = f->bytesPerLine(); }
Mat BallClasificationModule::QImage2Mat_ROI(QImage *image, Rectangle<int> region, int outFormat) { int w = image->width(), h = image->height(); int x0 = region.xleft, y0 = region.ytop; int x1 = std::min(w - 1, x0 + region.width), y1 = std::min(h - 1, y0 + region.height); // qDebug() << y1-y0 << "--" << x1-x0; Mat imgout(y1-y0,x1-x0,outFormat); uchar *pixels = imgout.data; uchar *img_b = image->bits(); int formato_imagen = image->format(); int bpl_img = image->bytesPerLine(); int bpl_out = imgout.step; // qDebug() << "Formato Imagen: " << formato_imagen; //8UC4 a 8UC1 if(formato_imagen == 5 && outFormat== CV_8UC1) { // qDebug() << "Salida en gris"; for(int x=x0; x<x1; x++) { for(int y=y0;y<y1;y++) { int pos = y*bpl_img +x*4; uchar B = img_b[pos+0]; uchar G = img_b[pos+1]; uchar R = img_b[pos+2]; uchar gray = (B+G+R)/3; // qDebug() << "[x,y] = " << x << "." << y; pixels[(y-y0)*bpl_out+(x-x0)+0] = gray; } } } else if(formato_imagen == 5 && outFormat== CV_8UC3) { // qDebug() << "Salida a Color"; for(int x=x0; x<x1; x++) { for(int y=y0;y<y1;y++) { int pos = y*bpl_img +x*4; uchar B = img_b[pos+0]; uchar G = img_b[pos+1]; uchar R = img_b[pos+2]; pixels[(y-y0)*bpl_out+(x-x0)*3+0] = B; pixels[(y-y0)*bpl_out+(x-x0)*3+1] = G; pixels[(y-y0)*bpl_out+(x-x0)*3+2] = R; } } } else if(formato_imagen == 3 && outFormat== CV_8UC1) { // qDebug() << "Salida en gris"; for(int x=x0; x<x1; x++) { for(int y=y0;y<y1;y++) { int pos = y*bpl_img +x; uchar G = img_b[pos]; pixels[(y-y0)*bpl_out+(x-x0)+0] = G; } } } else qDebug() << "Formato no soportado "; return imgout; }
void run(const std::string& filename, bool highpass) { // A gray image cv::Mat_<float> img = cv::imread(filename, CV_LOAD_IMAGE_GRAYSCALE); //Pad the image with borders using copyMakeBorders. Use getOptimalDFTSize(A+B-1). See G&W page 251,252 and 263 and dft tutorial. (Typicly A+B-1 ~ 2A is used) int rows = cv::getOptimalDFTSize(2*img.rows); int cols = cv::getOptimalDFTSize(2*img.cols); int imgRows = img.rows; int imgCols = img.cols; cv::copyMakeBorder(img,img,0,rows-img.rows,0,cols-img.cols,cv::BORDER_CONSTANT,cv::Scalar(0)); //Copy the gray image into the first channel of a new 2-channel image of type Mat_<Vec2f>, e.g. using merge(), save it in img_dft //The second channel should be all zeros. cv::Mat_<float> imgs[] = {img.clone(), cv::Mat_<float>(img.rows, img.cols, 0.0f)}; cv::Mat_<cv::Vec2f> img_dft; cv::merge(imgs, 2, img_dft); // Compute DFT cv::dft(img_dft, img_dft); // Split cv::split(img_dft, imgs); // Compute magnitude/phase cv::Mat_<float> magnitude, phase; cv::cartToPolar(imgs[0], imgs[1], magnitude, phase); // Shift quadrants for viewability dftshift(magnitude); // Logarithm of magnitude cv::Mat_<float> magnitudel; // Output image for HPF cv::Mat_<float> imgout; if(highpass) { // High-pass filter: remove the low frequency parts in the middle of the spectrum const int sizef = 50; magnitude(cv::Rect(magnitude.cols/2-sizef/2, magnitude.rows/2-sizef/2, sizef, sizef)) = 0.0f; // Take logarithm of modified magnitude magnitudel = magnitude + 1.0f; cv::log(magnitudel, magnitudel); // Shift back quadrants of the spectrum dftshift(magnitude); // Compute complex DFT output from magnitude/phase cv::polarToCart(magnitude, phase, imgs[0], imgs[1]); // Merge DFT into one image and restore cv::merge(imgs, 2, img_dft); cv::dft(img_dft, imgout, cv::DFT_INVERSE + cv::DFT_SCALE + cv::DFT_REAL_OUTPUT); //Cut away the borders imgout = imgout(cv::Rect(0,0,imgCols,imgRows)); } else { // Take logarithm of magnitude magnitudel = magnitude + 1.0f; cv::log(magnitudel, magnitudel); } // Show cv::normalize(img, img, 0.0, 1.0, CV_MINMAX); cv::normalize(magnitudel, magnitudel, 0.0, 1.0, CV_MINMAX); cv::normalize(phase, phase, 0.0, 1.0, CV_MINMAX); namedWindow("Input",WINDOW_NORMAL); namedWindow("Magnitude",WINDOW_NORMAL); namedWindow("Output",WINDOW_NORMAL); cv::imshow("Input", img); cv::imshow("Magnitude", magnitudel); if(highpass) { cv::normalize(imgout, imgout, 0.0, 1.0, CV_MINMAX); cv::imshow("Output", imgout); } cv::waitKey(); }