Split an image into multiple images based on result from cv2.HoughLinesRegion of Interest opencv pythonHow to return multiple values from a function?Split Strings into words with multiple word boundary delimitersPeak detection in a 2D arraySplit string with multiple delimiters in PythonSelect rows from a DataFrame based on values in a column in pandasPython cv2 HoughLines grid line detectioncv2.HoughLinesP on a skeletonized imageDetecting lines in an image using OpenCV with PythonHow do I transform the values of an accumulator [Hough Transformation] back to a line on a canvas?QImage - Process finished with exit code -1073741819 (0xC0000005)

Java - What do constructor type arguments mean when placed *before* the type?

Do all polymers contain either carbon or silicon?

The One-Electron Universe postulate is true - what simple change can I make to change the whole universe?

Can a Gentile theist be saved?

Superhero words!

Meta programming: Declare a new struct on the fly

Giant Toughroad SLR 2 for 200 miles in two days, will it make it?

How do I repair my stair bannister?

Science Fiction story where a man invents a machine that can help him watch history unfold

Freedom of speech and where it applies

Golf game boilerplate

Lifted its hind leg on or lifted its hind leg towards?

Is there a good way to store credentials outside of a password manager?

Simple recursive Sudoku solver

Simulating a probability of 1 of 2^N with less than N random bits

Why is delta-v is the most useful quantity for planning space travel?

Are taller landing gear bad for aircraft, particulary large airliners?

What was required to accept "troll"?

Organic chemistry Iodoform Reaction

What is the term when two people sing in harmony, but they aren't singing the same notes?

Could solar power be utilized and substitute coal in the 19th century?

Lightning Web Component - do I need to track changes for every single input field in a form

How can a jailer prevent the Forge Cleric's Artisan's Blessing from being used?

My boss asked me to take a one-day class, then signs it up as a day off



Split an image into multiple images based on result from cv2.HoughLines


Region of Interest opencv pythonHow to return multiple values from a function?Split Strings into words with multiple word boundary delimitersPeak detection in a 2D arraySplit string with multiple delimiters in PythonSelect rows from a DataFrame based on values in a column in pandasPython cv2 HoughLines grid line detectioncv2.HoughLinesP on a skeletonized imageDetecting lines in an image using OpenCV with PythonHow do I transform the values of an accumulator [Hough Transformation] back to a line on a canvas?QImage - Process finished with exit code -1073741819 (0xC0000005)













0















I want to split this image into multiple images based on the black lines



enter image description here



I use cv2.HoughLines to get some lines, merging them to avoid overlapping lines.
And here my drawing code:



# After get lines from cv2.HoughLines()
for line in lines:

rho, theta = line
a = np.cos(theta)
b = np.sin(theta)
x0 = a * rho
y0 = b * rho
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))

cv2.line(image, (x1, y1), (x2, y2), (0, 200, 0), 2)

cv2.imwrite('results/result.jpg', image)


Here's the result:



enter image description here



I wonder how can I split the images into multiple small images with those green lines










share|improve this question


























    0















    I want to split this image into multiple images based on the black lines



    enter image description here



    I use cv2.HoughLines to get some lines, merging them to avoid overlapping lines.
    And here my drawing code:



    # After get lines from cv2.HoughLines()
    for line in lines:

    rho, theta = line
    a = np.cos(theta)
    b = np.sin(theta)
    x0 = a * rho
    y0 = b * rho
    x1 = int(x0 + 1000 * (-b))
    y1 = int(y0 + 1000 * (a))
    x2 = int(x0 - 1000 * (-b))
    y2 = int(y0 - 1000 * (a))

    cv2.line(image, (x1, y1), (x2, y2), (0, 200, 0), 2)

    cv2.imwrite('results/result.jpg', image)


    Here's the result:



    enter image description here



    I wonder how can I split the images into multiple small images with those green lines










    share|improve this question
























      0












      0








      0








      I want to split this image into multiple images based on the black lines



      enter image description here



      I use cv2.HoughLines to get some lines, merging them to avoid overlapping lines.
      And here my drawing code:



      # After get lines from cv2.HoughLines()
      for line in lines:

      rho, theta = line
      a = np.cos(theta)
      b = np.sin(theta)
      x0 = a * rho
      y0 = b * rho
      x1 = int(x0 + 1000 * (-b))
      y1 = int(y0 + 1000 * (a))
      x2 = int(x0 - 1000 * (-b))
      y2 = int(y0 - 1000 * (a))

      cv2.line(image, (x1, y1), (x2, y2), (0, 200, 0), 2)

      cv2.imwrite('results/result.jpg', image)


      Here's the result:



      enter image description here



      I wonder how can I split the images into multiple small images with those green lines










      share|improve this question














      I want to split this image into multiple images based on the black lines



      enter image description here



      I use cv2.HoughLines to get some lines, merging them to avoid overlapping lines.
      And here my drawing code:



      # After get lines from cv2.HoughLines()
      for line in lines:

      rho, theta = line
      a = np.cos(theta)
      b = np.sin(theta)
      x0 = a * rho
      y0 = b * rho
      x1 = int(x0 + 1000 * (-b))
      y1 = int(y0 + 1000 * (a))
      x2 = int(x0 - 1000 * (-b))
      y2 = int(y0 - 1000 * (a))

      cv2.line(image, (x1, y1), (x2, y2), (0, 200, 0), 2)

      cv2.imwrite('results/result.jpg', image)


      Here's the result:



      enter image description here



      I wonder how can I split the images into multiple small images with those green lines







      python cv2






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 8 at 8:22









      BlurieBlurie

      5918




      5918






















          2 Answers
          2






          active

          oldest

          votes


















          1














          Suppose image is the variable in which the image is read by opencv as an nd-array.



          image = cv2.imread(image_filepath)


          Now if lines is the variable assigned after the houghline transformation like :



          lines = cv2.HoughLinesP(...) 


          Get its's shape :



          a,b,c = lines.shape


          Initiate a variable to get the coordinates and append the bounding-boxes :



          line_coords_list = []
          for i in range(a):
          line_coords_list.append([(lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3])])


          Now, loop through the list of bounding boxes and crop the main image and write them with some filename :



          temp_img = image[start_y_coordinate : end_y_coordinate , start_x_coorinate : end_x_coordinate]
          temp_name = image_filepath[:-4] + "_"+str(start_y_coordinate )+"_"+str(end_y_coordinate)+ "_" + str(start_x_coorinate) + "_" + str(end_x_coordinate) + ".png"
          cv2.imwrite(temp_name, temp_img)


          If you are using cv2.HoughLines(...), then you probably have to find contours in the image using :



          _, blackAndWhite = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
          _,contours,h = cv2.findContours(blackAndWhite,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)


          and, then loop through the contours :



          for cnt in contours:
          x,y,w,h = cv2.boundingRect(cnt)
          line_coords_list.append((x,y,w,h))


          Here while finding contours the third and fourth items are width and height respectively. So end_y_coordinate = y+h and end_x_coordinate = x+w.






          share|improve this answer

























          • The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

            – Blurie
            Mar 8 at 8:51


















          0














          See "region of interest"

          (Region of Interest opencv python - StackOverflow)



          Read this to get x/y:

          (Hough Line Transform - Opencv Phyton Tutorials 1 documentation)






          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059236%2fsplit-an-image-into-multiple-images-based-on-result-from-cv2-houghlines%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            Suppose image is the variable in which the image is read by opencv as an nd-array.



            image = cv2.imread(image_filepath)


            Now if lines is the variable assigned after the houghline transformation like :



            lines = cv2.HoughLinesP(...) 


            Get its's shape :



            a,b,c = lines.shape


            Initiate a variable to get the coordinates and append the bounding-boxes :



            line_coords_list = []
            for i in range(a):
            line_coords_list.append([(lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3])])


            Now, loop through the list of bounding boxes and crop the main image and write them with some filename :



            temp_img = image[start_y_coordinate : end_y_coordinate , start_x_coorinate : end_x_coordinate]
            temp_name = image_filepath[:-4] + "_"+str(start_y_coordinate )+"_"+str(end_y_coordinate)+ "_" + str(start_x_coorinate) + "_" + str(end_x_coordinate) + ".png"
            cv2.imwrite(temp_name, temp_img)


            If you are using cv2.HoughLines(...), then you probably have to find contours in the image using :



            _, blackAndWhite = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
            _,contours,h = cv2.findContours(blackAndWhite,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)


            and, then loop through the contours :



            for cnt in contours:
            x,y,w,h = cv2.boundingRect(cnt)
            line_coords_list.append((x,y,w,h))


            Here while finding contours the third and fourth items are width and height respectively. So end_y_coordinate = y+h and end_x_coordinate = x+w.






            share|improve this answer

























            • The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

              – Blurie
              Mar 8 at 8:51















            1














            Suppose image is the variable in which the image is read by opencv as an nd-array.



            image = cv2.imread(image_filepath)


            Now if lines is the variable assigned after the houghline transformation like :



            lines = cv2.HoughLinesP(...) 


            Get its's shape :



            a,b,c = lines.shape


            Initiate a variable to get the coordinates and append the bounding-boxes :



            line_coords_list = []
            for i in range(a):
            line_coords_list.append([(lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3])])


            Now, loop through the list of bounding boxes and crop the main image and write them with some filename :



            temp_img = image[start_y_coordinate : end_y_coordinate , start_x_coorinate : end_x_coordinate]
            temp_name = image_filepath[:-4] + "_"+str(start_y_coordinate )+"_"+str(end_y_coordinate)+ "_" + str(start_x_coorinate) + "_" + str(end_x_coordinate) + ".png"
            cv2.imwrite(temp_name, temp_img)


            If you are using cv2.HoughLines(...), then you probably have to find contours in the image using :



            _, blackAndWhite = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
            _,contours,h = cv2.findContours(blackAndWhite,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)


            and, then loop through the contours :



            for cnt in contours:
            x,y,w,h = cv2.boundingRect(cnt)
            line_coords_list.append((x,y,w,h))


            Here while finding contours the third and fourth items are width and height respectively. So end_y_coordinate = y+h and end_x_coordinate = x+w.






            share|improve this answer

























            • The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

              – Blurie
              Mar 8 at 8:51













            1












            1








            1







            Suppose image is the variable in which the image is read by opencv as an nd-array.



            image = cv2.imread(image_filepath)


            Now if lines is the variable assigned after the houghline transformation like :



            lines = cv2.HoughLinesP(...) 


            Get its's shape :



            a,b,c = lines.shape


            Initiate a variable to get the coordinates and append the bounding-boxes :



            line_coords_list = []
            for i in range(a):
            line_coords_list.append([(lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3])])


            Now, loop through the list of bounding boxes and crop the main image and write them with some filename :



            temp_img = image[start_y_coordinate : end_y_coordinate , start_x_coorinate : end_x_coordinate]
            temp_name = image_filepath[:-4] + "_"+str(start_y_coordinate )+"_"+str(end_y_coordinate)+ "_" + str(start_x_coorinate) + "_" + str(end_x_coordinate) + ".png"
            cv2.imwrite(temp_name, temp_img)


            If you are using cv2.HoughLines(...), then you probably have to find contours in the image using :



            _, blackAndWhite = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
            _,contours,h = cv2.findContours(blackAndWhite,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)


            and, then loop through the contours :



            for cnt in contours:
            x,y,w,h = cv2.boundingRect(cnt)
            line_coords_list.append((x,y,w,h))


            Here while finding contours the third and fourth items are width and height respectively. So end_y_coordinate = y+h and end_x_coordinate = x+w.






            share|improve this answer















            Suppose image is the variable in which the image is read by opencv as an nd-array.



            image = cv2.imread(image_filepath)


            Now if lines is the variable assigned after the houghline transformation like :



            lines = cv2.HoughLinesP(...) 


            Get its's shape :



            a,b,c = lines.shape


            Initiate a variable to get the coordinates and append the bounding-boxes :



            line_coords_list = []
            for i in range(a):
            line_coords_list.append([(lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3])])


            Now, loop through the list of bounding boxes and crop the main image and write them with some filename :



            temp_img = image[start_y_coordinate : end_y_coordinate , start_x_coorinate : end_x_coordinate]
            temp_name = image_filepath[:-4] + "_"+str(start_y_coordinate )+"_"+str(end_y_coordinate)+ "_" + str(start_x_coorinate) + "_" + str(end_x_coordinate) + ".png"
            cv2.imwrite(temp_name, temp_img)


            If you are using cv2.HoughLines(...), then you probably have to find contours in the image using :



            _, blackAndWhite = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY_INV)
            _,contours,h = cv2.findContours(blackAndWhite,cv2.RETR_LIST ,cv2.CHAIN_APPROX_SIMPLE)


            and, then loop through the contours :



            for cnt in contours:
            x,y,w,h = cv2.boundingRect(cnt)
            line_coords_list.append((x,y,w,h))


            Here while finding contours the third and fourth items are width and height respectively. So end_y_coordinate = y+h and end_x_coordinate = x+w.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 8 at 9:32

























            answered Mar 8 at 8:36









            pistol2myheadpistol2myhead

            1,5061923




            1,5061923












            • The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

              – Blurie
              Mar 8 at 8:51

















            • The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

              – Blurie
              Mar 8 at 8:51
















            The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

            – Blurie
            Mar 8 at 8:51





            The cv2.HoughLines(...) return only rho and theta, I don't know how to get start_y_coordinate, start_y_coordinate, etc.

            – Blurie
            Mar 8 at 8:51













            0














            See "region of interest"

            (Region of Interest opencv python - StackOverflow)



            Read this to get x/y:

            (Hough Line Transform - Opencv Phyton Tutorials 1 documentation)






            share|improve this answer





























              0














              See "region of interest"

              (Region of Interest opencv python - StackOverflow)



              Read this to get x/y:

              (Hough Line Transform - Opencv Phyton Tutorials 1 documentation)






              share|improve this answer



























                0












                0








                0







                See "region of interest"

                (Region of Interest opencv python - StackOverflow)



                Read this to get x/y:

                (Hough Line Transform - Opencv Phyton Tutorials 1 documentation)






                share|improve this answer















                See "region of interest"

                (Region of Interest opencv python - StackOverflow)



                Read this to get x/y:

                (Hough Line Transform - Opencv Phyton Tutorials 1 documentation)







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 8 at 19:19

























                answered Mar 8 at 8:32









                busybytebusybyte

                195




                195



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid …


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059236%2fsplit-an-image-into-multiple-images-based-on-result-from-cv2-houghlines%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

                    Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

                    How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page