对灰度图像进行简单线性插补的函数(有未知bug)

    科技2023-12-16  102

     current_view存放图像数据,current_view2存放临时数据;

    图像大小TEMPLATE_X_SIZE TEMPLATE_Y_SIZE

    step_i step_j 存放圆心周围的相对位置,初始化时生成,由内第二层开始;

    LINE_THRESHOLD = 0.1; LINE_THRESHOLD_PIXEL = round(LINE_THRESHOLD * 1.0 * TEMPLATE_X_SIZE / 2 / SENSOR_RANGE; for(int i=2; i <= LINE_THRESHOLD_PIXEL; i++) { for(int m=0; m < i+1; m++) { step_i.resize(step_i.size()+1); step_i.push_back(m); step_j.resize(step_j.size()+1); step_j.push_back(-i); } for(int m=0; m < 2*i-1; m++) { step_i.resize(step_i.size()+1); step_i.push_back(i); step_j.resize(step_j.size()+1); step_j.push_back(m-i+1); } for(int m=0; m < i+1; m++) { step_i.resize(step_i.size()+1); step_i.push_back(i-m); step_j.resize(step_j.size()+1); step_j.push_back(i); } }

    如果两个像素之间的值大于零,则进行线性插补;

    //create lines to fill into nearby pixels void fill_line_view() { for (unsigned int i=0; i < TEMPLATE_SIZE; i++) current_view2[i] = current_view[i]; int steps_ij = step_i.size(); for(int i=0; i<TEMPLATE_X_SIZE; i++) for(int j=0; j<TEMPLATE_Y_SIZE; j++) { double data_ij = current_view[i + j * TEMPLATE_X_SIZE]; if(data_ij > 0) { int step = 0; for(; step < steps_ij; step++) { if((i + step_i[step]) < 0 || (j + step_j[step]) < 0 || (i + step_i[step]) >= TEMPLATE_X_SIZE || (j + step_j[step]) >= TEMPLATE_Y_SIZE) continue; double data_ij_ = current_view[i + step_i[step] + (j + step_j[step]) * TEMPLATE_X_SIZE]; if(data_ij_ <= 0) continue; double data_mid = (data_ij_ + data_ij)/2.0; int max_steps_line = max(abs(step_i[step]),abs(step_j[step]));// the pixels from center to current pixel, 2 , 3 ... for(int steps_line=1; steps_line < max_steps_line; steps_line++) { double pos_x = i + step_i[step]*1.0f*steps_line/max_steps_line; double pos_y = j + step_j[step]*1.0f*steps_line/max_steps_line; point_interpolation(pos_x, pos_y, data_mid); } } } else continue; } } void point_interpolation(double pos_x, double pos_y, double p_value)// line interpolation point into pixel { int pos_x1 = floor(pos_x);// int pos_x2 = ceil(pos_x); int pos_y1 = floor(pos_y); int pos_y2 = ceil(pos_y); double w_x, w_y; if(pos_x1==pos_x) w_x = 1.0; else w_x = pos_x2 - pos_x; if(pos_y1==pos_y) w_y = 1.0; else w_y = pos_y2 - pos_y; if(pos_x1<0) pos_x1 += TEMPLATE_X_SIZE; if(pos_x2<0) pos_x2 += TEMPLATE_X_SIZE; if(pos_y1<0) pos_y1 += TEMPLATE_Y_SIZE; if(pos_y2<0) pos_y2 += TEMPLATE_Y_SIZE; if(pos_x1>=TEMPLATE_X_SIZE) pos_x1 -= TEMPLATE_X_SIZE; if(pos_x2>=TEMPLATE_X_SIZE) pos_x2 -= TEMPLATE_X_SIZE; if(pos_y1>=TEMPLATE_Y_SIZE) pos_y1 -= TEMPLATE_Y_SIZE; if(pos_y2>=TEMPLATE_Y_SIZE) pos_y2 -= TEMPLATE_Y_SIZE; if(current_view2[pos_x1+pos_y1 * TEMPLATE_X_SIZE] < p_value) current_view2[pos_x1+pos_y1 * TEMPLATE_X_SIZE] += w_x * w_y *p_value; if(current_view2[pos_x1+pos_y2 * TEMPLATE_X_SIZE] < p_value) current_view2[pos_x1+pos_y2 * TEMPLATE_X_SIZE] += w_x * (1 - w_y) *p_value; if(current_view2[pos_x2+pos_y1 * TEMPLATE_X_SIZE] < p_value) current_view2[pos_x2+pos_y1 * TEMPLATE_X_SIZE] += (1 - w_x) * w_y *p_value; if(current_view2[pos_x2+pos_y2 * TEMPLATE_X_SIZE] < p_value) current_view2[pos_x2+pos_y2 * TEMPLATE_X_SIZE] += (1 - w_x) * (1 - w_y) *p_value; }

    显示图像需要用到opencv库

    Processed: 0.018, SQL: 9