Hudcan_ 2022-03-25 11:07 采纳率: 0%
浏览 27

请问双页图片如何分割

img

请问这种图片怎么根据书籍的中间分割成两张图片?
我尝试用opencv中的HoughLines方法根据直线查找,但是始终找不到中间的线
示例代码

package com.yxd.facesb;

import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.net.URL;

import static org.opencv.highgui.HighGui.imshow;
import static org.opencv.highgui.HighGui.waitKey;
import static org.opencv.imgcodecs.Imgcodecs.imread;
import static org.opencv.imgcodecs.Imgcodecs.imwrite;
import static org.opencv.imgproc.Imgproc.COLOR_RGB2GRAY;
import static org.opencv.imgproc.Imgproc.cvtColor;

public class OpenCv {
    public static void main(String[] args) throws Exception {

        System.setProperty("java.awt.headless", "false");
        System.out.println(System.getProperty("java.library.path"));
        // 加载动态库
        URL url = new URL("file:\\D:\\opencv\\opencv\\build\\java\\x64\\opencv_java455.dll");
        System.load(url.getPath());

        Mat canny2 = new Mat();
        Mat canny3 = new Mat();
        Mat canny4 = new Mat();
        Mat canny5 = new Mat();

        // 读取图像
        Mat srcImage = imread("e:\\aa.jpg");
        Mat dstImage = srcImage.clone();
        Imgproc.Canny(srcImage, dstImage, 50, 200, 3, false);

        Imgcodecs.imwrite("F:\\dst3.jpg", dstImage);

        Imgproc.dilate(dstImage,canny4,canny3,new Point(),2);
        Imgcodecs.imwrite("F:\\canny4.jpg", canny4);
        Imgproc.erode(canny4, canny5, canny3);
        Imgcodecs.imwrite("F:\\dst5.jpg", canny5);

        Imgproc.watershed(srcImage,canny5);

        Mat storage = new Mat();
        Imgproc.HoughLines(canny5, storage, 1, Math.PI / 180, 500, 0, 0, 0, 10);
        for (int x = 0; x < storage.rows(); x++)
        {
            double[] vec = storage.get(x, 0);

            double rho = vec[0];
            double theta = vec[1];

            Point pt1 = new Point();
            Point pt2 = new Point();

            double a = Math.cos(theta);
            double b = Math.sin(theta);

            double x0 = a * rho;
            double y0 = b * rho;

            pt1.x = Math.round(x0 + 1000 * (-b));
            pt1.y = Math.round(y0 + 1000 * (a));
            pt2.x = Math.round(x0 - 1000 * (-b));
            pt2.y = Math.round(y0 - 1000 * (a));

            if (theta >= 0)
            {
                Imgproc.line(srcImage, pt1, pt2, new Scalar(255, 255, 255, 255), 1, Imgproc.LINE_4, 0);
            }
        }
        Imgcodecs.imwrite("F:\\dst2.jpg", srcImage);
    }

}
  • 写回答

1条回答 默认 最新

  • 赵4老师 2022-03-30 13:14
    关注

    仅供参考:

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include "opencv2/imgproc/imgproc_c.h"
    #include "opencv2\highgui\highgui_c.h"
    using namespace std;
    using namespace cv;
    Mat img,smallImg,gray,bw;
    vector<Vec4i> hierarchy;
    vector<vector<Point> > contours;
    int threshval=128;
    Rect r;
    Rect maxrect,brect;
    int idx,n;
    const static Scalar colors[15]={
        CV_RGB(  0,  0,128),
        CV_RGB(  0,128,  0),
        CV_RGB(  0,128,128),
        CV_RGB(128,  0,  0),
        CV_RGB(128,  0,128),
        CV_RGB(128,128,  0),
        CV_RGB(128,128,128),
        CV_RGB(160,160,160),
        CV_RGB(  0,  0,255),
        CV_RGB(  0,255,  0),
        CV_RGB(  0,255,255),
        CV_RGB(255,  0,  0),
        CV_RGB(255,  0,255),
        CV_RGB(255,255,  0),
        CV_RGB(255,255,255),
    };
    Scalar color;
    void gamma_correct(Mat& img, Mat& dst, double gamma) {
        Mat tmp;
    
        img.convertTo(tmp, CV_32FC1, 1.0/255.0, 0.0);
        pow(tmp, gamma, tmp);
        tmp.convertTo(dst , CV_8UC1,     255.0, 0.0);
    }
    int main() {
        cvNamedWindow("display",1);
        img=imread("image.jpg",1);
        r.x     =img.cols*1/10;
        r.y     =img.rows*1/5;
        r.width =img.cols*8/10;
        r.height=img.rows*3/5;
        smallImg=img(r);
        cvtColor(smallImg,gray,CV_BGR2GRAY);
    //  medianBlur(gray,gray,5);
        equalizeHist(gray,gray);
        gamma_correct(gray,gray,4.0);
        imshow("display",gray);
        waitKey(0);
    
        bw=(gray>threshval);
        imshow("display",bw);
        waitKey(0);
    
    //  Mat Structure1=getStructuringElement(MORPH_RECT   ,Size(6,6));
        Mat Structure1=getStructuringElement(MORPH_ELLIPSE,Size(6,6));
        dilate(bw,bw,Structure1, Point(-1,-1));
        imshow("display",bw);
        waitKey(0);
    
        Mat Structure0=getStructuringElement(MORPH_RECT   ,Size(3,3));
        Mat Structure0=getStructuringElement(MORPH_ELLIPSE,Size(3,3));
        erode(bw,bw,Structure0,Point(-1,-1));
        imshow("display",bw);
        waitKey(0);
    
        findContours(bw,contours,hierarchy,RETR_EXTERNAL,CHAIN_APPROX_SIMPLE);
        if (!contours.empty()&&!hierarchy.empty()) {
            idx=0;
            n=0;
            vector<Point> approx;
            for (;idx>=0;idx=hierarchy[idx][0]) {
                color=colors[idx%15];
    //          drawContours(smallImg,contours,idx,color,1,8,hierarchy);
                approxPolyDP(Mat(contours[idx]), approx, arcLength(Mat(contours[idx]), true)*0.005, true);//0.005为将毛边拉直的系数
                const Point* p = &approx[0];
                int m=(int)approx.size();
                polylines(smallImg, &p, &m, 1, true, color);
                circle(smallImg,Point(p[0].x,p[0].y),3,color);
                circle(smallImg,Point(p[1].x,p[1].y),2,color);
                for (int i=2;i<m;i++) circle(smallImg,Point(p[i].x,p[i].y),1,color);
                n++;
                if (1==n) {
                    maxrect=boundingRect(Mat(contours[idx]));
                } else {
                    brect=boundingRect(Mat(contours[idx]));
                    CvRect mr;
                    mr.x=maxrect.x;
                    mr.y=maxrect.y;
                    mr.width=maxrect.width;
                    mr.height=maxrect.height;
                    CvRect br;
                    br.x=brect.x;
                    br.y=brect.y;
                    br.width=brect.width;
                    br.height=brect.height;
                    maxrect=cvMaxRect(&mr,&br);
                }
            }
            circle(smallImg,Point(maxrect.x+maxrect.width/2,maxrect.y+maxrect.height/2),2,CV_RGB(255,0,0));
        }
        imshow("display",smallImg);
        waitKey(0);
        cvDestroyWindow("display");
        return 0;
    }
    
    
    
    评论

报告相同问题?

问题事件

  • 创建了问题 3月25日

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀