13 * other.a21 + a23 * other.a22 + a33
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? * other.a23, a13 * other.a31 + a23 * other.a32 + a33 * other.a33));
? return result;
}
void PerspectiveTransform::transformPoints(vector &points) {
? int max = points.size();
? for (int i = 0; i < max; i += 2) {
? ? float x = points[i];
? ? float y = points[i + 1];
? ? float denominator = a13 * x + a23 * y + a33;
? ? points[i] = (a11 * x + a21 * y + a31) / denominator;
? ? points[i + 1] = (a12 * x + a22 * y + a32) / denominator;
? }
}
对一张透视图片变换回正面图的效果:
int main(){
?Mat img=imread("boy.png");
?int img_height = img.rows;
?int img_width = img.cols;
?Mat img_trans = Mat::zeros(img_height,img_width,CV_8UC3);
?PerspectiveTransform tansform = PerspectiveTransform::quadrilateralToQuadrilateral(
? 0,0,
? img_width-1,0,
? 0,img_height-1,
? img_width-1,img_height-1,
? 150,250, // top left
? 771,0, // top right
? 0,1023,// bottom left
? 650,1023
? );
?vector ponits;
?for(int i=0;i? for(int j=0;j? ?ponits.push_back(j);
? ?ponits.push_back(i);
? }
?}
?tansform.transformPoints(ponits);
?for(int i=0;i? uchar*? t= img_trans.ptr(i);
? for (int j=0;j? ?int tmp = i*img_width+j;
? ?int x = ponits[tmp*2];
? ?int y = ponits[tmp*2+1];
? ?if(x<0||x>(img_width-1)||y<0||y>(img_height-1))
? ? continue;
? ?uchar* p = img.ptr(y);
? ?t[j*3] = p[x*3];
? ?t[j*3+1] = p[x*3+1];
? ?t[j*3+2] = p[x*3+2];
? }
?}
?imwrite("trans.png",img_trans);
?return 0;
}
