学习仿射变换时用到waitKey()实现图片旋转显示
waitKey(30)
for(;;){
if (waitKey(30) >= 0)//返回30ms内按下的键值,否则返回-1. break; }
其中()内的值表示等待时间(ms),如代码中表示等待30ms,waitKey(30)返回值为30ms内键盘按下的返回值(键盘中每个按键对应一个ASCII码,见)
如果没有按键按下,则返回-1,所以不会跳出循环waitKey(0)
表示一直等待用户按键事件发生
int main(){ Mat src = imread("lyj.png"); imshow("this is 林宥嘉",src); Point2f srcTri[] = { Point2f(0,0), Point2f(src.cols - 1,0), Point2f(0,src.rows - 1), }; Point2f dstTri[] = { Point2f(0,src.rows*0.33f), Point2f(src.cols*0.85f,src.rows*0.25f), Point2f(src.cols*0.15f,src.rows*0.9f), }; //compute affine matrix Mat warp_mat = getAffineTransform(srcTri, dstTri); Mat dst, dst2; warpAffine(src, dst, warp_mat, src.size(), INTER_LINEAR, BORDER_CONSTANT, Scalar()); for (int i = 0; i < 3; i++) circle(dst, dstTri[i], 5, Scalar(255, 0, 255), -1, 16); imshow("Affine Transform Test", dst); waitKey(0); //Rotation for (int frame = 0;; ++frame) { Point2f center=Point2f(src.cols*0.5f, src.rows*0.5f); double angle = frame * 3 % 360, scale = (cos((angle - 60)*PI / 180) + 1.05)*0.8; Mat rot_mat = getRotationMatrix2D(center, angle, scale); warpAffine(src, dst2, rot_mat, src.size(), INTER_LINEAR, BORDER_CONSTANT, Scalar()); imshow("Affine Transform Test2", dst2); if (waitKey(30) >= 0)//返回30ms内按下的键值,否则返回-1. break; } return 0;}