opencv倾斜矫正

/****************倾斜校正子程序*****************/
//函数名称:IplImage *Rotate(IplImage *RowImage
//功能:对每行数字进行倾斜校正
//入口参数:行图像RowImage
//出口参数:旋转后的图像RotateRow
/********************************************/
IplImage *Rotate(IplImage *RowImage)
{
    //建立储存边缘检测结果图像canImage
//     IplImage *canImage=cvCreateImage(CvSize(200,300),IPL_DEPTH_8U,1);
    IplImage *canImage=cvCreateImage(cvGetSize(RowImage),IPL_DEPTH_8U,1);
    //进行边缘检测
    cvCanny(RowImage,canImage,30,200,3);
    //进行hough变换
    CvMemStorage *storage=cvCreateMemStorage();
    CvSeq *lines=NULL;
    lines=cvHoughLines2(canImage,storage,CV_HOUGH_STANDARD,1,CV_PI/180,20,0,0);
    //统计与竖直夹角<30度的直线个数以及其夹角和
    int numLine=0;
    float sumAng=0.0;
    for(int i=0;i<lines->total;i++)
    {
        float *line=(float *)cvGetSeqElem(lines,i);
        float theta=line[1];  //获取角度 为弧度制
        if(theta<30*CV_PI/180 || (CV_PI-theta)<30*CV_PI/180 )
        {
            numLine++;
            sumAng=sumAng+theta;
        }
    }
    //计算出平均倾斜角,anAng为角度制
    float avAng=(sumAng/numLine)*180/CV_PI;
    //获取二维旋转的仿射变换矩阵
    CvPoint2D32f center;
    center.x=float (RowImage->width/2.0);
    center.y=float (RowImage->height/2.0);
    float m[6];
    CvMat M = cvMat( 2, 3, CV_32F, m );
    cv2DRotationMatrix( center,avAng,1, &M);
    //建立输出图像RotateRow
    double a=sin(sumAng/numLine);
    double b=cos(sumAng/numLine);
    int width_rotate=int (RowImage->height*fabs(a)+RowImage->width*fabs(b));
    int height_rotate=int (RowImage->width*fabs(a)+RowImage->height*fabs(b));
    IplImage *RotateRow=cvCreateImage(cvSize(width_rotate,height_rotate),IPL_DEPTH_8U,1);
    //变换图像,并用黑色填充其余值
    m[2]+=(width_rotate-RowImage->width)/2;
    m[5]+=(height_rotate-RowImage->height)/2;
    cvWarpAffine(RowImage,RotateRow, &M,CV_INTER_LINEAR+CV_WARP_FILL_OUTLIERS,cvScalarAll(0));
    //释放
    cvReleaseImage(&canImage);
    cvReleaseMemStorage(&storage);
    return RotateRow;
}
int main(int argc, char *argv[])
{
//    QCoreApplication a(argc, argv);
    Mat imgMat = imread("20160802.jpeg");//const String* filename);
// Mat imgMat = imread("DSCN6533.png");//const String* filename);
    if(imgMat.empty())return -1; //是否加载成功
   if(!imgMat.data)return -1;//判断是否有数据
//    IplImage pImg= IplImage(imgMat);
    IplImage *pImg = cvLoadImage("20160802.jpeg");
    IplImage *tImg =Rotate(pImg);
//    IplImage* img = cvCreateImage(cvGetSize(mat),8,1);
//   cvGetImage(matI,img);
   cvSaveImage("rice1.png",tImg);
    //建立储存边缘检测结果图像canImage
    return 0;//a.exec();
}


上述代码会报错:

 Assertion failed (src.type() == dst.type()) in cvWarpAffine, file /home/lbg/softs/opencv-3.0.0/modules/imgproc/src/imgwarp.cpp, line 6369
terminate called after throwing an instance of 'cv::Exception'
  what():  /home/lbg/softs/opencv-3.0.0/modules/imgproc/src/imgwarp.cpp:6369: error: (-215) src.type() == dst.type() in function cvWarpAffine



下面是python的:


__author__ = 'Administrator'
import sys
import numpyas np
import cv2as cv
import math
from argparseimport ArgumentParser
FILENAME= "1.150000001.png";
 
HOUGH_VOTE= 100
GRAY_THRESH= 150
 
#srcImgOrg : the source image
#srcImgGray : the source image with gray scale
defcalcRotAngle(srcImgOrg,srcImgGray):
angleD = 0
opWidth = cv.getOptimalDFTSize(srcImgGray.shape[1])
opHeight = cv.getOptimalDFTSize(srcImgGray.shape[0])
 
padded = cv.copyMakeBorder(srcImgGray, 0, opWidth - srcImgGray.shape[1] , 0, opHeight- srcImgGray.shape[0], cv.BORDER_CONSTANT);
plane = np.zeros(padded.shape,dtype=np.float32)
planes = [padded,plane]
#Merge into a double-channel image
comImg = cv.merge(planes)
cv.dft(comImg,comImg)
cv.split(comImg, planes)
 
planes[0]= cv.magnitude(planes[0], planes[1]);
magMat = planes[0]
magMat += np.ones(magMat.shape)
cv.log(magMat,magMat);
 
cx = magMat.shape[1] / 2;
cy = magMat.shape[0] / 2
q0 = magMat[0:cx,0: cy ]
q1 = magMat[cx:,0: cy]
q2 = magMat[0:cx, cy:]
q3 = magMat[cx:,cy:]
c1 = np.vstack((q3,q2))
c2 = np.vstack((q1,q0))
magMat2 = np.hstack((c1,c2))
 
cv.normalize(magMat2, magMat,0, 1,cv.NORM_MINMAX);
magMat = cv.resize(magMat,(magMat.shape[0] / 2,magMat.shape[1]/2))
magMat = magMat * 255
magMat = cv.threshold(magMat,GRAY_THRESH,255,cv.THRESH_BINARY)[1].astype(np.uint8)
lines = cv.HoughLines(magMat,1,np.pi/180,HOUGH_VOTE);
#cv.imshow("mag_binary", magMat);
#lineImg = np.ones(magMat.shape,dtype=np.uint8)
angle = 0
if lines!= None andlen(lines) != 0:
for linein lines[0]:
#print line
rho = line[0]
theta = line[1]
if (theta< (np.pi/4. ))or (theta > (3.*np.pi/4.0)):
print'Vertical line , rho : %f , theta : %f'%(rho,theta)
pt1 = (int(rho/np.cos(theta)),0)
pt2 = (int((rho-magMat.shape[0]*np.sin(theta))/np.cos(theta)),magMat.shape[0])
#cv.line( lineImg, pt1, pt2, (255))
angle = theta
else:
print'Horiz line , rho : %f , theta : %f'%(rho,theta)
pt1 = (0,int(rho/np.sin(theta)))
pt2 = (magMat.shape[1], int((rho-magMat.shape[1]*np.cos(theta))/np.sin(theta)))
#cv.line(lineImg, pt1, pt2, (255), 1)
angle = theta + np.pi / 2
#cv.imshow('lineImg',lineImg)
#Find the proper angel
if angle> (np.pi / 2):
angle = angle - np.pi
 
#Calculate the rotation angel
#The image has to be square,
#so that the rotation angel can be calculate right
print'angle : %f'% angle
 
#print srcImgOrg.shape
alpha = float(srcImgOrg.shape[1])/ float(srcImgOrg.shape[0])
print'alpha : %f'% alpha
if alpha> 1:
angleT = srcImgOrg.shape[1] * np.tan(angle)/ srcImgOrg.shape[0];
angleD = np.arctan(angleT) * 180/ np.pi;
else:
angleD = angle * 180 / np.pi
print'angleD : %f'% angleD
return angleD
 
defrotImage(srcImgOrg,angleD):
size = srcImgOrg.shape
centerPnt = (srcImgOrg.shape[1] /2,srcImgOrg.shape[0] / 2)
rotMat = cv.getRotationMatrix2D(centerPnt,angleD,scale=1.);
resultImg = cv.warpAffine(srcImgOrg,rotMat,(size[1],size[0]));
 
#cv.imshow('srcImgOrg',srcImgOrg);
#resultImg = cv.resize(resultImg,(resultImg.shape[0] / 2,resultImg.shape[1]/2))
#cv.imshow("resultImg",resultImg);
fileParts = fileName.split('.')
fileParts[-2]= fileParts[-2]+ '-r'
file= '.'.join(fileParts)
print"file name : %s"% file
 
ret = cv.imwrite(file,resultImg)
 
defhandleImage(fileName):
srcImgOrg = cv.imread(fileName)
srcImgGray = cv.imread(fileName,cv.IMREAD_GRAYSCALE).astype(np.float32);
angle = calcRotAngle(srcImgOrg,srcImgGray)
if angle> 0:
rotImage(srcImgOrg,angle)
 
defmain():
p = ArgumentParser(usage='it is usage tip',description='this is a usage tip')
p.add_argument('--file',default="./",help='input file name')
args = p.parse_args()
#print args.file
handleImage(args.file)
 
if__name__ == '__main__':
main()
#rotImage(FILENAME)
cv.waitKey( 0)

参考:https://github.com/lyzh1688/SlantCorrection

opencv图像校正(摄像头校正)

需要事先标定:

http://download.csdn.net/download/hs5530hs/9046567

需要事先标定:

http://blog.csdn.net/zht9961020/article/details/7036786

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI视觉网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值