{"id":917,"date":"2018-10-03T01:24:56","date_gmt":"2018-10-03T01:24:56","guid":{"rendered":"http:\/\/muthu.co\/?p=917"},"modified":"2021-05-24T02:59:35","modified_gmt":"2021-05-24T02:59:35","slug":"draw-bounding-box-around-contours-skimage","status":"publish","type":"post","link":"http:\/\/write.muthu.co\/draw-bounding-box-around-contours-skimage\/","title":{"rendered":"Draw bounding box around contours – skimage"},"content":{"rendered":"\n

The basic idea is to find the Xmin, Xmax, Ymin, Ymax of each identified coordinates of the contour and then create a rectangle using it. Lets understand this with an example:<\/p>\n\n\n\n

Import the necessary libraries, read the input file, convert it to grayscale and plot it.<\/p>\n\n\n\n

from skimage.measure import find_contours\nfrom skimage.io import imread\nimport matplotlib.pyplot as plt\nfrom skimage.color import rgb2gray\n\norig_img = imread('resources\/stars.jpg')\ngray_img = rgb2gray(orig_img)\nplt.imshow(gray_img,interpolation='nearest', cmap=plt.cm.gray)\n\n<\/code><\/pre>\n\n\n\n
\"\"<\/a><\/figure><\/div>\n\n\n\n

Now we use the skimage find_contours method to identify the contours in my input image<\/p>\n\n\n\n

contours = find_contours(gray_img, 0.8)\n\nfig, ax = plt.subplots()\nax.imshow(gray_img, interpolation='nearest', cmap=plt.cm.gray)\n\nfor n, contour in enumerate(contours):\n    ax.plot(contours[n][:, 1], contours[n][:, 0], linewidth=2)\n\nplt.show()<\/code><\/pre>\n\n\n\n
\"\"<\/a><\/figure><\/div>\n\n\n\n

The contours in the image are groups x,y coordinates from which we need to find Xmin, Xmax, Ymin, Ymax which can be used to draw the bounding box.<\/p>\n\n\n\n

import numpy as np\nfrom skimage.draw import polygon_perimeter\n\nbounding_boxes = []\n\nfor contour in contours:\n    Xmin = np.min(contour[:,0])\n    Xmax = np.max(contour[:,0])\n    Ymin = np.min(contour[:,1])\n    Ymax = np.max(contour[:,1])\n    \n    bounding_boxes.append([Xmin, Xmax, Ymin, Ymax])\n\n\nwith_boxes  = np.copy(gray_img)\n\nfor box in bounding_boxes:\n    #[Xmin, Xmax, Ymin, Ymax]\n    r = [box[0],box[1],box[1],box[0], box[0]]\n    c = [box[3],box[3],box[2],box[2], box[3]]\n    rr, cc = polygon_perimeter(r, c, with_boxes.shape)\n    with_boxes[rr, cc] = 1 #set color white\n\nplt.imshow(with_boxes, interpolation='nearest', cmap=plt.cm.gray)\nplt.show()\n\n<\/code><\/pre>\n\n\n\n
\"\"<\/a><\/figure><\/div>\n\n\n\n


That’s all! You can find the full
notebook here.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

The basic idea is to find the Xmin, Xmax, Ymin, Ymax of each identified coordinates of the contour and then create a rectangle using it. Lets understand this with an example: Import the necessary libraries, read the input file, convert it to grayscale and plot it. Now we use the skimage find_contours method to identify […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38],"tags":[47],"_links":{"self":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/917"}],"collection":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/comments?post=917"}],"version-history":[{"count":3,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/917\/revisions"}],"predecessor-version":[{"id":1880,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/posts\/917\/revisions\/1880"}],"wp:attachment":[{"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/media?parent=917"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/categories?post=917"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/write.muthu.co\/wp-json\/wp\/v2\/tags?post=917"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}