// Loads pattern images for detection int LoadPattern(const char* filename, vector<Mat>& library, int& patternCount) { Mat img = imread(filename, 0); if (img.cols != img.rows){ return -1; printf("Not a square pattern"); } int msize = PAT_SIZE; Mat src(msize, msize, CV_8UC1); Point2f center((msize - 1) / 2.0f, (msize - 1) / 2.0f); Mat rot_mat(2, 3, CV_32F); resize(img, src, Size(msize, msize)); Mat subImg = src(Range(msize / 4, 3 * msize / 4), Range(msize / 4, 3 * msize / 4)); library.push_back(subImg); rot_mat = getRotationMatrix2D(center, 90, 1.0); for (int i = 1; i<4; i++){ Mat dst = Mat(msize, msize, CV_8UC1); rot_mat = getRotationMatrix2D(center, -i * 90, 1.0); warpAffine(src, dst, rot_mat, Size(msize, msize)); Mat subImg = dst(Range(msize / 4, 3 * msize / 4), Range(msize / 4, 3 * msize / 4)); library.push_back(subImg); } patternCount++; return 1; }
bool ASM_Gaze_Tracker::calculatePupilCenter(){ Mat leftEyeImg,rightEyeImg,cropped; if (isTrackingSuccess == false) { return false; } canthusPts = vector<Point2f>(tracker.points.begin(),tracker.points.begin()+4); nosePts = vector<Point2f>(tracker.points.begin()+4,tracker.points.begin()+6); if (canthusPts[2].x < canthusPts[0].x && canthusPts[0].x < canthusPts[1].x && canthusPts[1].x < canthusPts[3].x) { } else { return false; } eyePairTileAngle = calculateEyePairTileAngle(canthusPts); glabellaPoint= caculateEyePairCenter(canthusPts); rotationMatrix = getRotationMatrix2D(glabellaPoint, eyePairTileAngle, 1.0); Mat Mback = getRotationMatrix2D(glabellaPoint, -eyePairTileAngle, 1.0); vector<Point2f> rotatedCanthusPts = rotatePointsByRotationMatrix(canthusPts, rotationMatrix); vector<Point2f> rotatedNosePts = rotatePointsByRotationMatrix(nosePts, rotationMatrix); float eyePairRectWidth =abs(rotatedCanthusPts[2].x - rotatedCanthusPts[3].x)+1; Size2f eyePairRectSize(eyePairRectWidth,eyePairRectWidth/7); Rect cropRect(Point2f(glabellaPoint.x-eyePairRectWidth/2,glabellaPoint.y -eyePairRectWidth/14.0f),eyePairRectSize); warpAffine(im, rotated_img, rotationMatrix, im.size(),CV_INTER_LINEAR); getRectSubPix(rotated_img, eyePairRectSize, glabellaPoint, cropped); Rect leftEyeRect = Rect(0,0,rotatedCanthusPts[0].x-rotatedCanthusPts[2].x,eyePairRectSize.height); Rect rightEyeRect = Rect(rotatedCanthusPts[1].x-rotatedCanthusPts[2].x,0,rotatedCanthusPts[3].x-rotatedCanthusPts[1].x,eyePairRectSize.height); if (leftEyeRect.area() < 50 || rightEyeRect.area()< 50) { return false; } Point2f leftEyeCenter, rightEyeCenter; // findEyeCenterByColorSegmentation(cropped(leftEyeRect), leftEyeCenter); // findEyeCenterByColorSegmentation(cropped(rightEyeRect), rightEyeCenter); // cout<<"debug"<<endl; boost::thread leftEyeThread(findEyeCenterByColorSegmentation, cropped(leftEyeRect), boost::ref(leftEyeCenter), 0.4,3,3,5); boost::thread rightEyeThread(findEyeCenterByColorSegmentation, cropped(rightEyeRect), boost::ref(rightEyeCenter), 0.4,3,3,5); leftEyeThread.join(); rightEyeThread.join(); leftEyeCenter += Point2f(leftEyeRect.tl().x,leftEyeRect.tl().y); leftEyeCenter += Point2f(cropRect.tl().x, cropRect.tl().y); rightEyeCenter += Point2f(rightEyeRect.tl().x,rightEyeRect.tl().y); rightEyeCenter += Point2f(cropRect.tl().x,cropRect.tl().y); leftEyePoint= rotatePointByRotationMatrix(leftEyeCenter, Mback); rightEyePoint= rotatePointByRotationMatrix(rightEyeCenter, Mback); isTrackingSuccess = true; return true; }
Mat deskew(Mat img, double angle) { bitwise_not(img, img); vector<Point> points; Mat_<uchar>::iterator it = img.begin<uchar>(); Mat_<uchar>::iterator end = img.end<uchar>(); for (; it != end; ++it) if (*it) points.push_back(it.pos()); RotatedRect box = minAreaRect(Mat(points)); Mat rot_mat = getRotationMatrix2D(box.center, angle, 1); Mat rotated; warpAffine(img, rotated, rot_mat, img.size(), INTER_CUBIC); Size box_size = box.size; if (box.angle < -45.) swap(box_size.width, box_size.height); Mat cropped; getRectSubPix(rotated, box_size, box.center, cropped); return cropped; }
PERF_TEST_P(TestWarpAffine, WarpAffine_ovx, Combine( Values(szVGA, sz720p, sz1080p), InterType::all(), BorderMode::all() ) ) { Size sz, szSrc(512, 512); int borderMode, interType; sz = get<0>(GetParam()); interType = get<1>(GetParam()); borderMode = get<2>(GetParam()); Scalar borderColor = Scalar::all(150); Mat src(szSrc, CV_8UC1), dst(sz, CV_8UC1); cvtest::fillGradient(src); if (borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1); Mat warpMat = getRotationMatrix2D(Point2f(src.cols / 2.f, src.rows / 2.f), 30., 2.2); declare.in(src).out(dst); TEST_CYCLE() warpAffine(src, dst, warpMat, sz, interType, borderMode, borderColor); #ifdef __ANDROID__ SANITY_CHECK(dst, interType == INTER_LINEAR ? 5 : 10); #else SANITY_CHECK(dst, 1); #endif }
void VideoCorrect::rotateImage(Mat& source, double angle) { Point2f src_center(source.cols/2.0F, source.rows/2.0F); Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0); Mat dst; warpAffine(source, source, rot_mat, source.size(), 1, 0, Scalar(255,255,255)); }
FastSymmetryDetector::FastSymmetryDetector( const Size image_size, const Size hough_size, const int rot_resolution ) { this->imageSize = image_size; this->center = Point2f( imageSize.width - 1.0, imageSize.height - 1.0 ) * 0.5; this->diagonal = hypotf( imageSize.width, imageSize.height ); this->rhoDivision = diagonal; this->rhoMax = hough_size.width; this->thetaMax = hough_size.height; rotMatrices.resize( thetaMax, Mat(2, 2, CV_32FC1) ); float thetaIncDeg = 180.0f / thetaMax; float half_theta_max = thetaMax * 0.5f; /* Pre calculate rotation matrices from -90 deg to 90 deg (actually to 89 deg) */ for( int t = 0; t < thetaMax; t++ ){ double angle = thetaIncDeg * ( t - half_theta_max ); Mat rotation = getRotationMatrix2D( Point2f(0.0, 0.0), angle, 1.0); rotation.convertTo( rotation, CV_32FC1 ); rotMatrices[t] = Mat( rotation, Rect(0, 0, 2, 2) ); rotMatrices[t].row(0) *= 0.5; } accum = Mat::zeros( thetaMax + 2, rhoMax, CV_32FC1 ); rotEdges = Mat::zeros( rhoDivision, diagonal, CV_32FC1 ); reRows.resize( rhoDivision ); }
void paperRegistration::warpImage(cv::Mat &rawImage, cv::Mat &image, bool camInUse) { if (rawImage.empty()) return; //cv::Mat image = rawImage.clone(); if (camInUse) { std::vector<cv::Point2f> point(2); point[0] = cvPoint(172,0); point[1] = cvPoint(490, 352); if (!warpMat.empty()) { cv::warpPerspective(rawImage, image, warpMat, cv::Size(1000,2000)); cv::perspectiveTransform(point, point, warpMat); } image = cv::Mat(image, cv::Rect(point[0], point[1])); cv::Mat blurrImg; cv::GaussianBlur(image, blurrImg, cv::Size(5,5), 3); cv::addWeighted(image, 1.6, blurrImg, -0.5, 0, image); //imwrite("cam1.png", image); } else { warpMat = getRotationMatrix2D(cv::Point2f(rawImage.cols/2.0, rawImage.rows/2.0), 180, 1); cv::warpAffine(rawImage, image, warpMat, rawImage.size()); cv::resize(image, image, cv::Size(rawImage.cols*imgRatio_, rawImage.rows*imgRatio_), 0, 0 ,cv::INTER_LINEAR); //imwrite("sim.png", image); } }
void rotateByAngle(Mat &dst, double angle) { Point2f pt(dst.cols / 2., dst.rows / 2.); Mat r = getRotationMatrix2D(pt, angle, 1); warpAffine(dst, dst, r, Size(dst.cols, dst.rows), INTER_CUBIC, BORDER_CONSTANT, Scalar(255, 255, 255, 0)); }
Mat Detector::rotateImage(const Mat& source, double angle) { Point2f src_center(source.cols/2.0F, source.rows/2.0F); Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0); Mat dst; warpAffine(source, dst, rot_mat, source.size()); return dst; }
Mat ImageTransformation::rotateImage(Mat originalImage, double angle) { Mat rotatedImage; Point2f pt(originalImage.cols/2., originalImage.rows/2.); Mat r = getRotationMatrix2D(pt, angle, 1.0); warpAffine(originalImage, rotatedImage, r, Size(originalImage.cols, originalImage.rows)); return rotatedImage; }
Mat rotate(Mat src, double angle) { Mat dst; Point2f pt(src.cols/2., src.rows/2.); Mat r = getRotationMatrix2D(pt, -angle, 1.0); warpAffine(src, dst, r, Size(src.cols, src.rows)); return dst; }
// Return the rotation matrices for each rotation void rotate(cv::Mat& src, double angle, cv::Mat& dst) { cv::Mat r = getRotationMatrix2D(cv::Point2f(), angle, 1.0); //4 coordinates of the image std::vector<cv::Point2f> corners(4); corners[0] = cv::Point2f(0, 0); corners[1] = cv::Point2f(0, src.rows); corners[2] = cv::Point2f(src.cols, 0); corners[3] = cv::Point2f(src.cols, src.rows); std::vector<cv::Point2f> cornersTransform(4); cv::transform(corners, cornersTransform, r); //Copy the 2x3 transformation matrix into a 3x3 transformation matrix cv::Mat H = cv::Mat::eye(3, 3, CV_64F); for(int i = 0; i < 2; i++) { for(int j = 0; j < 3; j++) { H.at<double>(i, j) = r.at<double>(i, j); } } double offsetX = 0.0, offsetY = 0.0, maxX = 0.0, maxY = 0.0; //Get max offset outside of the image and max width / height for(size_t i = 0; i < 4; i++) { if(cornersTransform[i].x < offsetX) { offsetX = cornersTransform[i].x; } if(cornersTransform[i].y < offsetY) { offsetY = cornersTransform[i].y; } if(cornersTransform[i].x > maxX) { maxX = cornersTransform[i].x; } if(cornersTransform[i].y > maxY) { maxY = cornersTransform[i].y; } } offsetX = -offsetX; offsetY = -offsetY; maxX += offsetX; maxY += offsetY; cv::Size size_warp(maxX, maxY); //Create the transformation matrix to be able to have all the pixels cv::Mat H2 = cv::Mat::eye(3, 3, CV_64F); H2.at<double>(0,2) = offsetX; H2.at<double>(1,2) = offsetY; warpPerspective(src, dst, H2*H, size_warp); }
//rotates about center cv::Mat ModelMaker::rotateImage(const Mat& source, double anglerad) { qDebug()<<"Angle in rad"<<anglerad; double angle = ((anglerad*180)/CV_PI); qDebug()<<"Angle in deg"<<angle; Point2f src_center(source.cols/2.0F, source.rows/2.0F); Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0); Mat dst; warpAffine(source, dst, rot_mat, source.size()); return dst; }
void FrameProcessor::processStereoFrame(const Mat & frameL, const Mat & frameR, Mat & pointCloud){ Mat disparityMap, disparityMapNormalized; Mat frameTransposedL, frameTransposedR, frameRemappedL, frameRemappedR, frameGrayscaleL, frameGrayscaleR; Mat rotMatL = cvCreateMat(2,3,CV_32FC1); Mat rotMatR = cvCreateMat(2,3,CV_32FC1); // Compute rotation matrix CvPoint2D32f centerL = cvPoint2D32f( frameL.cols/2, frameL.rows/2 ); rotMatL = getRotationMatrix2D( centerL, 90, 1 ); CvPoint2D32f centerR = cvPoint2D32f( frameR.cols/2, frameR.rows/2 ); rotMatR = getRotationMatrix2D( centerR, 90, 1 ); warpAffine(frameL, frameTransposedL, rotMatL, frameL.size() ); warpAffine(frameR, frameTransposedR, rotMatR, frameR.size() ); //transpose(frameL, frameTransposedL); //transpose(frameR, frameTransposedR); remap(frameTransposedL, frameRemappedL, rmap[0][0], rmap[0][1], CV_INTER_LINEAR); remap(frameTransposedR, frameRemappedR, rmap[1][0], rmap[1][1], CV_INTER_LINEAR); //imshow("LiveFeedL",frameTransposedL); //imshow("LiveFeedR",frameTransposedR); cvtColor(frameRemappedL, frameGrayscaleL, CV_RGB2GRAY); cvtColor(frameRemappedR, frameGrayscaleR, CV_RGB2GRAY); BlockMatcher( frameGrayscaleL, frameGrayscaleR, disparityMap, CV_32F); normalize(disparityMap, disparityMapNormalized, 0, 255, CV_MINMAX, CV_8U); imshow("Disparity", disparityMapNormalized); reprojectImageTo3D( disparityMap, pointCloud, Q, false); }
cv::Mat rotateImg (cv::Mat src, int angle) { cv::Point2f center(src.cols/2.0F, src.rows/2.0F); cv::Mat rot = getRotationMatrix2D(center, angle, 1.0); cv::Mat dst; cv::Rect bbox = cv::RotatedRect(center,src.size(), angle).boundingRect(); // adjust transformation matrix rot.at<double>(0,2) += bbox.width/2.0 - center.x; rot.at<double>(1,2) += bbox.height/2.0 - center.y; cv::warpAffine(src, dst, rot, bbox.size()); return dst; }
void myrotate(const Mat &src, Mat& dst, int angle) { Point2f src_center(0, 0); Mat rot_mat = getRotationMatrix2D(src_center, angle, 1.0); if ((angle / 90) % 2 == 0) warpAffine(src, dst, rot_mat, src.size()); else { Size dst_size= Size(src.rows, src.cols); warpAffine(src, dst, rot_mat, dst_size); } }
static void RotShapeInPlace( Shape& shape, // io double rot, // in: in-plane rotation angle in degrees, pos is anticlock double x, // in: rotation origin double y) // in { CV_Assert(rot >= -360 && rot <= 360); // sanity check, 360 is arb const MAT rotmat = getRotationMatrix2D(cv::Point2f(float(x), float(y)), rot, 1.); AlignShapeInPlace(shape, rotmat); }
////////////////////////////////////////////// // rotate picture (to align eyes-y) ////////////////////////////////////////////// Mat rotate(Mat& image, double angle, CvPoint centre) { Point2f src_center(centre.x, centre.y); // conversion en degre angle = angle*180.0/3.14157; //DEBUG printf("(D) rotate : rotating : %f° %d %d\n",angle, centre.x, centre.y); Mat rot_matrix = getRotationMatrix2D(src_center, angle, 1.0); Mat rotated_img(Size(image.size().height, image.size().width), image.type()); warpAffine(image, rotated_img, rot_matrix, image.size()); return (rotated_img); }
size_t Pattern::loadPattern(const char* filename){ if( !fexists(filename) ) { std::cerr<<"Unable to open "<<filename<<". Verify if the file exists and the rights are correctly set."<<std::endl; return EXIT_FAILURE; } Mat img = imread(filename,0); if(img.cols!=img.rows){ return -1; std::cerr << "Not a square pattern" << std::endl; } int msize = patternSize; Mat src(msize, msize, CV_8UC1); Point2f center((msize-1)/2.0f,(msize-1)/2.0f); Mat rot_mat(2,3,CV_32F); resize(img, src, Size(msize,msize)); Mat subImg = src(Range(msize/4,3*msize/4), Range(msize/4,3*msize/4)); Pattern::patternLibrary.push_back(subImg); rot_mat = getRotationMatrix2D( center, 90, 1.0); for (int i=1; i<patternNbRotation; i++){ Mat dst= Mat(msize, msize, CV_8UC1); rot_mat = getRotationMatrix2D( center, -i*90, 1.0); warpAffine( src, dst , rot_mat, Size(msize,msize)); Mat subImg = dst(Range(msize/4,3*msize/4), Range(msize/4,3*msize/4)); Pattern::patternLibrary.push_back(subImg); } patternCount++; return patternCount; }
GaussianKernel* GaussianKernel::CvRotate(float angle) { if (angle == 0) return this; Point2f center(_zero, _zero); Mat rotationMatrix = getRotationMatrix2D(center, angle, 1.0); Mat rotated = Mat::zeros(_matKernel.rows, _matKernel.cols, _matKernel.type()); warpAffine(_matKernel, rotated, rotationMatrix, _matKernel.size()); return new GaussianKernel(rotated); }
static DetPar ImgDetParToRoiFrame( const DetPar& detpar, // in const Rect& rect_roi) // in { DetPar detpar_roi(detpar); detpar_roi.x -= rect_roi.x; detpar_roi.y -= rect_roi.y; Shape eyemouth_shape(5, 2, 0.); if (Valid(detpar_roi.lex)) { eyemouth_shape(0, IX) -= rect_roi.x; eyemouth_shape(0, IY) -= rect_roi.y; } if (Valid(detpar_roi.rex)) { eyemouth_shape(1, IX) -= rect_roi.x; eyemouth_shape(1, IY) -= rect_roi.y; } if (Valid(detpar_roi.mouthx)) { eyemouth_shape(2, IX) -= rect_roi.x; eyemouth_shape(2, IY) -= rect_roi.y; } if (Valid(detpar.rot) && detpar.rot) { // rotate eyes and mouth const MAT rotmat = getRotationMatrix2D(cv::Point2f(float(detpar_roi.x), float(detpar_roi.y)), -detpar.rot, 1.); TransformShapeInPlace(eyemouth_shape, rotmat); } if (Valid(detpar.lex)) { detpar_roi.lex = eyemouth_shape(0, IX); detpar_roi.ley = eyemouth_shape(0, IY); } if (Valid(detpar.rex)) { detpar_roi.rex = eyemouth_shape(1, IX); detpar_roi.rey = eyemouth_shape(1, IY); } if (Valid(detpar.mouthx)) { detpar_roi.mouthx = eyemouth_shape(2, IX); detpar_roi.mouthy = eyemouth_shape(2, IY); } return detpar_roi; }
Shape ImgShapeToRoiFrame( // return shape in ROI frame const Shape& shape, // in: shape in image frame const DetPar& detpar_roi, // in: detpar wrt the ROI const DetPar& detpar) // in { Shape outshape(ShiftShape(shape, detpar_roi.x - detpar.x, detpar_roi.y - detpar.y)); if (Valid(detpar.rot) && detpar.rot) { const MAT rotmat = getRotationMatrix2D(cv::Point2f(float(detpar_roi.x), float(detpar_roi.y)), -detpar.rot, 1.); TransformShapeInPlace(outshape, rotmat); } return outshape; }
int test_rotate_float() { #ifdef _MSC_VER cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1); #else cv::Mat matSrc = cv::imread("test_images/lena.png", 1); #endif if (!matSrc.data) { std::cout << "read image fail" << std::endl; return -1; } cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY); matSrc.convertTo(matSrc, CV_32FC1); double angle = -50.0; for (int interpolation = 0; interpolation < 5; interpolation++) { fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0); fbc::Mat_<float, 1> mat(matSrc.rows, matSrc.cols, matSrc.data); fbc::Mat_<float, 1> rotate_dst; fbc::rotate(mat, rotate_dst, center, angle, true, interpolation); // Compute a rotation matrix with respect to the center of the image cv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0); // Get the rotation matrix with the specifications above cv::Mat mat_rot_ = getRotationMatrix2D(center_, angle, 1.0); cv::Mat rotate_dst_; cv::warpAffine(matSrc, rotate_dst_, mat_rot_, matSrc.size(), interpolation); assert(rotate_dst.step == rotate_dst_.step && rotate_dst.rows == rotate_dst_.rows); for (int y = 0; y < rotate_dst.rows; y++) { const fbc::uchar* p = rotate_dst.ptr(y); const uchar* p_ = rotate_dst_.ptr(y); for (int x = 0; x < rotate_dst.step; x++) { assert(p[x] == p_[x]); } } } return 0; }
Shape RoiShapeToImgFrame( // return shape in image frame const Shape& shape, // in: shape in roi frame const Image& face_roi, // in const DetPar& detpar_roi, // in: detpar wrt the ROI const DetPar& detpar) // in: detpar wrt the image { Shape outshape(shape.clone()); if (IsLeftFacing(detpar.eyaw)) outshape = FlipShape(outshape, face_roi.cols); if (Valid(detpar.rot) && detpar.rot) { const MAT rotmat = getRotationMatrix2D(cv::Point2f(float(detpar_roi.x), float(detpar_roi.y)), detpar.rot, 1.); TransformShapeInPlace(outshape, rotmat); } return ShiftShape(outshape, detpar.x - detpar_roi.x, detpar.y - detpar_roi.y); }
cv::Mat RotateOP::execute_current(const cv::Mat& img, const vector<string>& fields) { cv::Mat ret; if (!is_init()) { LOG(ERROR) << "RotateOp is not initialized"; } else { int angle = angle_; if (angle_fno_ > 0 && angle_fno_ <= fields.size()) { angle = std::stoi(fields[angle_fno_ - 1]); } cv::Point2f center(img.cols/2.0F, img.rows/2.0F); cv::Mat rot = getRotationMatrix2D(center, angle, 1.0); cv::Rect bbox = cv::RotatedRect(center, img.size(), angle).boundingRect(); rot.at<double>(0, 2) += bbox.width/2.0 - center.x; rot.at<double>(1, 2) += bbox.height/2.0 - center.y; warpAffine(img, ret, rot, bbox.size(), cv::INTER_CUBIC); } return ret; }
Mat CControl::GetSubsetImg(RotatedRect n_rotatedrect) { // cout<<n_rotatedrect<<endl; //根据旋转矩形,从nimgraw中获得新图像。 //获得边界矩形。 Rect n_boundrect=n_rotatedrect.boundingRect(); //将边界在原图像上画出。 Point2f vertices[4]; n_rotatedrect.points(vertices); // Mat n_imgraw; // nImgRaw.copyTo(n_imgraw); // for (int i = 0; i < 4; i++) // line(n_imgraw, vertices[i], vertices[(i+1)%4], Scalar(0,255,0)); // imshow("raw",n_imgraw); //获得子图像。 Mat n_boundmatemp=nImgRaw(n_boundrect); Mat n_boundmat; n_boundmatemp.copyTo(n_boundmat); // imshow("prerotated",n_boundmat); //获得旋转变换参数矩阵 //这里的中心不再是原图像的中心,而是子图的中心。(原中心-左上角) Point2f n_pt1=n_rotatedrect.center; Point2f n_pt2=n_boundrect.tl(); Point2f n_ptcenter=n_pt1-n_pt2; Mat n_rotatematparam=getRotationMatrix2D(n_ptcenter,n_rotatedrect.angle,1.0); //进行仿射变换。 Mat n_rotatedMat; warpAffine(n_boundmat, n_rotatedMat,n_rotatematparam,n_boundmat.size(),INTER_CUBIC); //截取子图像。 Mat n_croppedmat; Size n_subsize(nControlOptions.nWidth,nControlOptions.nHeight); getRectSubPix(n_rotatedMat,n_subsize,n_ptcenter,n_croppedmat); // imshow("rotated",n_croppedmat); // waitKey(0); return n_croppedmat; }
vector<Point2f> Train::rotateImage(Mat &image, Mat &out, float angle, float scale) { int borderSize = 40; int iRows = image.rows >> 1; int iCols = image.cols >> 1; int maxRC = max(iRows, iCols); int maxSize = (int)(sqrt(pow(maxRC, 2.0) * 2) + borderSize + 2) << 1; Mat tmp = Mat::zeros(maxSize, maxSize, image.type()); int nRows = maxSize >> 1; int nCols = maxSize >> 1; Rect middle(Point( nCols - iCols, nRows - iRows), Size(image.cols, image.rows)); vector<Point2f> corners; Point tl = middle.tl(); Point br = middle.br(); corners.push_back(Point2f(tl.x, tl.y)); corners.push_back(Point2f(tl.x, tl.y) + Point2f(image.cols, 0)); corners.push_back(Point2f(br.x, br.y)); corners.push_back(Point2f(tl.x, tl.y) + Point2f(0, image.rows)); vector<Point2f> outCorners(corners.size()); image.copyTo(tmp(middle)); Point2f center(nRows, nCols); Mat rot = getRotationMatrix2D(center, -angle, scale); warpAffine(tmp, out, rot,Size(maxSize, maxSize)); rotateCorners(corners, outCorners, center, angle, scale ); return outCorners; }
PERF_TEST_P( TestWarpPerspective, WarpPerspective, Combine( Values( szVGA, sz720p, sz1080p ), InterType::all(), BorderMode::all() ) ) { Size sz, szSrc(512, 512); int borderMode, interType; sz = get<0>(GetParam()); interType = get<1>(GetParam()); borderMode = get<2>(GetParam()); Scalar borderColor = Scalar::all(150); Mat src(szSrc,CV_8UC4), dst(sz, CV_8UC4); cvtest::fillGradient(src); if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1); Mat rotMat = getRotationMatrix2D(Point2f(src.cols/2.f, src.rows/2.f), 30., 2.2); Mat warpMat(3, 3, CV_64FC1); for(int r=0; r<2; r++) for(int c=0; c<3; c++) warpMat.at<double>(r, c) = rotMat.at<double>(r, c); warpMat.at<double>(2, 0) = .3/sz.width; warpMat.at<double>(2, 1) = .3/sz.height; warpMat.at<double>(2, 2) = 1; declare.in(src).out(dst); TEST_CYCLE() warpPerspective( src, dst, warpMat, sz, interType, borderMode, borderColor ); #ifdef __ANDROID__ SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10); #else SANITY_CHECK(dst, 1); #endif }
/*Ben rotate te imazhit sipas nje kendi qe kontrollohet me ane te nje trackbari (-180 <= kendi <= 180) dhe e shfaq kete imazh ne dritaren me emer @param emriDritares */ void Editim::rotullo(Mat& imazhOrig, string emriDritares) { int vleraSliderRotate = 180; createTrackbar("Rotate", emriDritares, &vleraSliderRotate, 360); while (true){ double kendi = vleraSliderRotate-180; //hiqet 180 per te marre vlera -180 deri 180 imazhOrig = fotoOrigjinale.clone(); //sigurohet qe rotullimi te aplikohet mbi imazhin origjinal //perndryshe kemi rrotullime te pafundme int gjatesia = max(imazhOrig.cols, imazhOrig.rows); //permasa e imazhit pas rrotullimit Point2f pikaBaze(gjatesia/2.0, gjatesia/2.0); //pika sipas te ciles behet rrotullimi (qendra) Mat matriceRrotullimi = getRotationMatrix2D(pikaBaze, kendi, 1.0); //llogaritet matrica //me te cilen behet rrotullimi //transformohet imazhi sipas kesaj matrice warpAffine(imazhOrig, imazhOrig, matriceRrotullimi, Size(gjatesia, gjatesia)); imshow(emriDritares, imazhOrig); int tastIshtypur = waitKey(50); if (tastIshtypur == 27) { cout << "Rrotullimi caktivizohet. " <<endl; break; } } return; }
//This method should be always called BEFORE any other method call. It is not called during constructor for efficiency. //It processes the original image to generate "processedImg" which is the actual image the class will work with. //It applies: rescaling, rotation, equalization, filtering and color transformations. //Should be called before extracCharacteristicPoints(). If not, it will be automatically called. inline bool Face::init () { Mat& img = this->originalImg; Mat gray, dstnImg( cvRound (img.rows/scale), cvRound(img.cols/scale), CV_8UC1 ); //Color conversion to grayscale. cvtColor( img, gray, CV_BGR2GRAY ); //Resizing with the given scale. resize( gray, dstnImg, dstnImg.size(), 0, 0, INTER_LINEAR ); //Equalizing the histogram. equalizeHist( dstnImg, dstnImg ); //Gaussian Filter to get rid of noise or excesive definition. Bluring. if (this->bluringEnabled) GaussianBlur( dstnImg, dstnImg, Size(3, 3), 2, 2); //Rotation of the image. this->rMat = getRotationMatrix2D(this->rotationCenter, this->rotationAngle, 1); Mat smallImg2; dstnImg.copyTo(smallImg2); warpAffine(smallImg2, dstnImg, this->rMat, dstnImg.size()); //Generation of inverse rotation matrix for future uses. See transformPoint(). invertAffineTransform(this->rMat,this->irMat); //Storing the final preprocessed image ready to be analized for face search and inspection. this->processedImg = dstnImg; //We indicate the object has been initialized so the rest of its API is available. this->faceFound = false; //We have to set this to false since the "processedImg" has changed. return (this->initialized = true); }