南极最酷小企鹅 2019年05月09日
自学图像处理,有没有大哥给指点一下?区分两种插值那部分有点没看明白?
#include <iostream> #include <vector> #include <opencv2/core/core.hpp>#include <opencv2/highgui/highgui.hpp>using namespace cv;using namespace std;void findNearPoint(int height, int width, int cHeight, int cWidth, int vec[], int x, int y);void resize(IplImage* src, IplImage* dst, int mode);void generateHistogramEqualizationImage(IplImage* input, int rgb);int main(int argc, char *argv[]){ if (argc == 3) { string imgPath = string(argv[1]); double zoom = atof(argv[2]); cout << "放大系数:" << zoom << endl; Mat originalMatImg = imread(imgPath.c_str()); IplImage iplImage = IplImage(originalMatImg); double x = iplImage.width*zoom; double y = iplImage.height*zoom; IplImage *dst = cvCreateImage(cvSize(x, y), iplImage.depth, iplImage.nChannels); resize(&iplImage, dst, CV_INTER_LINEAR); for (int i = 0; i < dst->nChannels; ++i) generateHistogramEqualizationImage(dst, i); Mat finalMatImg = cvarrToMat(dst); namedWindow("原图"); imshow("原图", originalMatImg); namedWindow("结果图片"); imshow("结果图片", finalMatImg); imwrite("result.png", finalMatImg); waitKey(6000); return 0; } else { cout << "Command: \n main.exe img.png zoomOut" << endl; }}void findNearPoint(int height, int width, int cHeight, int cWidth, int vec[], int x, int y){ if (cHeight + x < height && cWidth + y < width && cHeight + x >= 0 && cWidth + y >= 0) { vec[0] = cHeight + x; vec[1] = cWidth + y; } if (cHeight + x >= height) { vec[0] = height; } if (cHeight + x < 0) { vec[0] = 0; } if (cWidth + y >= width) { vec[1] = width; } if (cWidth + y < 0) { vec[1] = 0; }}void resize(IplImage* src, IplImage* dst, int mode){ int height_src = src->height; int width_src = src->width; int step_src = src->widthStep / sizeof(uchar); int channels = src->nChannels; uchar* data_src = (uchar *)src->imageData; int height_dst = dst->height; int width_dst = dst->width; int step_dst = dst->widthStep / sizeof(uchar); uchar* data_dst = (uchar *)dst->imageData; switch (mode) { case CV_INTER_NN: { for (int i = 0; i < height_dst; ++i) { for (int j = 0; j < width_dst; ++j) { int mapHeight = i * ((float)height_src / (float)height_dst); int mapWidth = j * ((float)width_src / (float)width_dst); for (int k = 0; k < channels; ++k) { data_dst[i*step_dst + j * channels + k] = data_src[mapHeight*step_src + mapWidth * channels + k]; } } } break; } case CV_INTER_LINEAR: { for (int i = 0; i < height_dst; ++i) { for (int j = 0; j < width_dst; ++j) { float mapHeight = i * ((float)height_src / (float)height_dst); float mapWidth = j * ((float)width_src / (float)width_dst); int mapIHeight = (int)mapHeight; int mapIWidth = (int)mapWidth; float u = mapHeight - mapIHeight; float v = mapWidth - mapIWidth; int vec_0[2] = { 0,0 }; int vec_1[2] = { 0,0 }; int vec_2[2] = { 0,0 }; int vec_3[2] = { 0,0 }; findNearPoint(height_src, width_src, mapIHeight, mapIWidth, vec_0, 0, 0);//f(x,y) findNearPoint(height_src, width_src, mapIHeight, mapIWidth, vec_1, 1, 0);//f(x+1,y) findNearPoint(height_src, width_src, mapIHeight, mapIWidth, vec_2, 0, 1);//f(x,y+1) findNearPoint(height_src, width_src, mapIHeight, mapIWidth, vec_3, 1, 1);//f(x+1,y+1) for (int k = 0; k < channels; ++k) { data_dst[i*step_dst + j * channels + k] = data_src[vec_0[0] * step_src + vec_0[1] * channels + k] * (1 - u) * (1 - v) + data_src[vec_1[0] * step_src + vec_1[1] * channels + k] * (1 - u) * v + data_src[vec_2[0] * step_src + vec_2[1] * channels + k] * u * (1 - v) + data_src[vec_3[0] * step_src + vec_3[1] * channels + k] * u * v; } } } break; } }}void generateHistogramEqualizationImage(IplImage* input, int rgb){ int height = input->height; int width = input->width; int step = input->widthStep / sizeof(uchar); int channels = input->nChannels; uchar* data1 = (uchar *)input->imageData; vector<int> countPix(256); vector<float> percentPix(256); int wholePixel = height * width; //count the pixel for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { countPix[data1[i*step + j * channels + rgb]]++; } } //percent of the pixel for (int i = 0; i < countPix.size(); ++i) { int wholeForwardPixel = 0; for (int j = 0; j <= i; ++j) { wholeForwardPixel += countPix[j]; } percentPix[i] = (float)wholeForwardPixel / (float)wholePixel; } // data[i*step+j*channels+k] = 111; for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { data1[i*step + j * channels + rgb] = percentPix[data1[i*step + j * channels + rgb]] * 255; } }}
...全文
173 点赞 收藏 4
写回复
4 条回复

还没有回复,快来抢沙发~

发动态
发帖子
C++ 语言
创建于2007-09-28

3.1w+

社区成员

24.8w+

社区内容

C++ 语言相关问题讨论,技术干货分享
社区公告
暂无公告