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

                    Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

                    2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived

                    Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme