目录
目标检测旋转增强源码
c++旋转图片
Mat t,f;
transpose(image,t);
flip(t,f,1); // code = 0 - x axis; 1 - y ; -1 - both
f就是转置+flip之后的结果了。
这个也是对的,没测:
https://www.jb51.net/article/215604.htm
dst_11 好像是对的:
https://www.jb51.net/article/221395.htm
int rotateImage(const cv::Mat &image, cv::Mat &dst, const double angle, const int mode)
{
cv::Mat dst_1,M;
int h = image.rows;
int w = image.cols;
M = cv::getRotationMatrix2D(cv::Point(w / 2, h / 2), 270, 1.0);
cv::warpAffine(image, dst, M, image.size());
double cos = abs(M.at<double>(0, 0));
double sin = abs(M.at<double>(0, 1));
int new_w = cos * w + sin * h;
int new_h = cos * h + sin * w;
M.at<double>(0, 2) += (new_w / 2.0 - w / 2);
M.at<double>(1, 2) += (new_h / 2.0 - h / 2);
cv::warpAffine(image, dst_1, M, cv::Size(new_w, new_h), cv::INTER_LINEAR, 0, cv::Scalar(255, 255, 0));
imshow("dst", dst);
imshow("dst_11", dst_1);
cv::waitKey(0);
//mode = 0 ,Keep the original image size unchanged
//mode = 1, Change the original image size to fit the rotated scale, padding with zero
return 0;
}
c++只能旋转90度,180度,270度
上面这段话大概意思就是rotate函数能够按照顺时针的方法以三种不同的角度进行旋转,三种角度分别是90度,180度,270度。
rotate参数是取枚举类型RotateFlags中的值,0表示90度,1表示180度,2表示270度,
下面是顺时针旋转180度示例:
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main() {
Mat frame, clone_frame;
frame = imread("D:/cat.jpg", 3);
clone_frame = frame.clone();
double scale = 0.5;
resize(clone_frame, clone_frame, Size(int(frame.cols * scale),
int(frame.rows * scale)),
0, 0, INTER_AREA);
cv::rotate(clone_frame, clone_frame, 1);
imshow("rotate_180_frame", clone_frame);
waitKey(0);
return 0;
}
原文链接:https://blog.csdn.net/qq_35140742/article/details/119906220
python旋转图片
左右旋转90度,测试ok
import cv2
# 顺时针旋转90度
def Rotate_90_r(img):
trans_img = cv2.transpose(img)
new_img = cv2.flip(trans_img, 1)
return new_img
# 逆时针旋转90度
def Rotate_90_l(img):
trans_img = cv2.transpose( img )
new_img = cv2.flip( trans_img, 0 )
return new_img
def test(img_path):
img = cv2.imread(img_path)
cv2.imshow('raw', img)
clock90_img = Rotate_90(img)
cv2.imshow( 'Rotate90', clock90_img )
clock_90_img = Rotate90(img)
cv2.imshow('Rotate_90', clock_90_img)
resize_img = cv2.resize(img,(int(img.shape[1]*.5),int(img.shape[0]*.5)))
cv2.imshow('resize_img', resize_img)
if __name__ == '__main__':
test('mi.jpg')
cv2.waitKey(0)
cv2.destroyAllWindows()
旋转90 180 270度:
image = cv2.rotate(src, cv2.cv2.ROTATE_90_CLOCKWISE)
ew = cv2.rotate(img, cv2.ROTATE_180)
image = cv2.rotate(src, cv2.ROTATE_90_COUNTERCLOCKWISE)
旋转后原图不会少,图像会变大:
转自:opencv正确的实现图像旋转 - 星河赵 - 博客园
单位是角度,不是弧度
def rotate_bound(image, angle):
# grab the dimensions of the image and then determine the
# center
(h, w) = image.shape[:2]
(cX, cY) = (w / 2, h / 2)
# grab the rotation matrix (applying the negative of the
# angle to rotate clockwise), then grab the sine and cosine
# (i.e., the rotation components of the matrix)
M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
cos = np.abs(M[0, 0])
sin = np.abs(M[0, 1])
# compute the new bounding dimensions of the image
nW = int((h * sin) + (w * cos))
nH = int((h * cos) + (w * sin))
# adjust the rotation matrix to take into account translation
M[0, 2] += (nW / 2) - cX
M[1, 2] += (nH / 2) - cY
# perform the actual rotation and return the image
return cv2.warpAffine(image, M, (nW, nH))
if __name__ == '__main__':
img = cv2.imread('img/8883.jpg')
while True:
lnum = random.uniform(-180, 180)
img_t= rotate_bound(img,lnum)
cv2.imshow("img_t", img_t)
cv2.waitKey()
continue
这个旋转矩形,会自动裁剪头尾,不能用
import numpy as np #1
import argparse #2
import imutils #3
import cv2 #4
ap = argparse.ArgumentParser() #5
ap.add_argument("-i", "--image", required = True,
help = "Path to the image") #6
args = vars(ap.parse_args()) #7
image = cv2.imread(args["image"]) #8
cv2.imshow("Original", image) #9
(h, w) = image.shape[:2] #10
center = (w // 2, h // 2) #11
M = cv2.getRotationMatrix2D(center, 45, 1.0) #12
rotated = cv2.warpAffine(image, M, (w, h)) #13
cv2.imshow("Rotated by 45 Degrees", rotated) #14
M = cv2.getRotationMatrix2D(center, -90, 1.0) #15
rotated = cv2.warpAffine(image, M, (w, h)) #16
cv2.imshow("Rotated by -90 Degrees", rotated) #17
rotated = imutils.rotate(image, 180) #18
cv2.imshow("Rotated by 180 Degrees", rotated) #19
cv2.waitKey(0) #20
————————————————
版权声明:本文为CSDN博主「ShellCollector」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/jacke121/article/details/106278894