c++图像缩放

    xiaoxiao2021-03-25  196

    //resize// void m_resize(Mat& src, Mat &dst, int cols, int rows) { // if(src.cols==cols&&src.rows==rows){ // dst.create(cols,rows); // src.convertTo(dst); // return; // } // Mat aa( rows, cols, CV_8UC3, Scalar(0,255,0) );//其实是2*6的矩阵,因为每个元素有3个通道。 double scale_x = (double)src.cols / cols; double scale_y = (double)src.rows / rows; // dst.create(cols,rows); uchar* dataDst = dst.data; int stepDst = dst.step; uchar* dataSrc = src.data; int stepSrc = src.step; int iWidthSrc = src.cols; int iHiehgtSrc = src.rows; for (int j = 0; j < dst.rows; ++j) { float fy = (float)((j + 0.5) * scale_y - 0.5); int sy = (int)(fy>=0?fy:fy-1); fy -= sy; sy = std::min(sy, iHiehgtSrc - 2); sy = std::max(0, sy); short cbufy[2]; cbufy[0] = double2short((1.f - fy) * 2048); //溢出保护 cbufy[1] = 2048 - cbufy[0]; for (int i = 0; i < dst.cols; ++i) { float fx = (float)((i + 0.5) * scale_x - 0.5); int sx = (int)(fx>=0?fx:fx-1); fx -= sx; if (sx < 0) { fx = 0, sx = 0; } if (sx >= iWidthSrc - 1) { fx = 0, sx = iWidthSrc - 2; } short cbufx[2]; cbufx[0] = double2short((1.f - fx) * 2048); cbufx[1] = 2048 - cbufx[0]; int channels=src.channels(); for (int k = 0; k < channels; ++k) { *(dataDst+ j*stepDst + channels*i + k) = (*(dataSrc + sy*stepSrc + channels*sx + k) * cbufx[0] * cbufy[0] + *(dataSrc + (sy+1)*stepSrc + channels*sx + k) * cbufx[0] * cbufy[1] + *(dataSrc + sy*stepSrc + channels*(sx+1) + k) * cbufx[1] * cbufy[0] + *(dataSrc + (sy+1)*stepSrc + channels*(sx+1) + k) * cbufx[1] * cbufy[1]) >> 22; } } } }

    int main(int argc, char* argv[]) { double t = (double)cvGetTickCount(); Mat image = imread("2.jpg",0); Mat fhog_feature(image.rows*1.05, image.cols*2, CV_8UC1, Scalar(0,255,0) );//其实是2*6的矩阵,因为每个元素有3个通道。 m_resize(image,fhog_feature, image.cols*2,image.rows*1.05); imshow("feat", fhog_feature); // 算法过程 t = (double)cvGetTickCount() - t; printf( "run time = %gms\n", t/(cvGetTickFrequency()*1000) ); waitKey(); return 0;

    }

    转载请注明原文地址: https://ju.6miu.com/read-1024.html

    最新回复(0)