Getting bounding boxes on words instead of letters











up vote
0
down vote

favorite












I am trying to put bounding boxes on each word which are there in a document. I have succeeded in putting bounding boxes on more or less each letter, but am unable to put bounding boxes on each word. I am using the code below for the same



im_ns = cv.imread('image.jpg')
gray = cv.cvtColor(im_ns,cv.COLOR_BGR2GRAY)
blurred_g = cv.GaussianBlur(gray,(11,11),0)
ret, th1 = cv.threshold(blurred_g,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
th3_1 = cv.adaptiveThreshold(th3,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
plt.figure(figsize=(30,25))
plt.imshow(th3_1)
th3_er = cv.erode(th3_1,None,iterations=1)
th3_di = cv.dilate(th3_er,None,iterations=1)
labels = measure.label(th3_di1, neighbors=8, background=255)
mask = np.zeros(th3_di1.shape, dtype="uint8")
for lab in np.unique(labels):
if lab == 0:
continue

labelMask = np.zeros(th3_di.shape, dtype="uint8")
labelMask[labels == lab] = 255
numPixels = cv.countNonZero(labelMask)

if numPixels > 13:
mask = cv.add(mask, labelMask)

cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = contours.sort_contours(cnts)[0]

t = th3_di.copy()
for (i, c) in enumerate(cnts):
(x,y,w,h) = cv.boundingRect(c)
cv.rectangle(t,(x,y),(x+w,y+h),(0,255),2)

cv.imshow("Image", t)
cv.waitKey(10000)
cv.destroyAllWindows()


The output of the above code can be seen in the link below
Image with bounding box










share|improve this question






















  • Maybe you can do morphological dilation to merge the letters using a small structure element. Then the connected components are words. Since there is blank space between words, it's less likely that different words are merged.
    – Kaiwen Chang
    Nov 23 at 13:47















up vote
0
down vote

favorite












I am trying to put bounding boxes on each word which are there in a document. I have succeeded in putting bounding boxes on more or less each letter, but am unable to put bounding boxes on each word. I am using the code below for the same



im_ns = cv.imread('image.jpg')
gray = cv.cvtColor(im_ns,cv.COLOR_BGR2GRAY)
blurred_g = cv.GaussianBlur(gray,(11,11),0)
ret, th1 = cv.threshold(blurred_g,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
th3_1 = cv.adaptiveThreshold(th3,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
plt.figure(figsize=(30,25))
plt.imshow(th3_1)
th3_er = cv.erode(th3_1,None,iterations=1)
th3_di = cv.dilate(th3_er,None,iterations=1)
labels = measure.label(th3_di1, neighbors=8, background=255)
mask = np.zeros(th3_di1.shape, dtype="uint8")
for lab in np.unique(labels):
if lab == 0:
continue

labelMask = np.zeros(th3_di.shape, dtype="uint8")
labelMask[labels == lab] = 255
numPixels = cv.countNonZero(labelMask)

if numPixels > 13:
mask = cv.add(mask, labelMask)

cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = contours.sort_contours(cnts)[0]

t = th3_di.copy()
for (i, c) in enumerate(cnts):
(x,y,w,h) = cv.boundingRect(c)
cv.rectangle(t,(x,y),(x+w,y+h),(0,255),2)

cv.imshow("Image", t)
cv.waitKey(10000)
cv.destroyAllWindows()


The output of the above code can be seen in the link below
Image with bounding box










share|improve this question






















  • Maybe you can do morphological dilation to merge the letters using a small structure element. Then the connected components are words. Since there is blank space between words, it's less likely that different words are merged.
    – Kaiwen Chang
    Nov 23 at 13:47













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I am trying to put bounding boxes on each word which are there in a document. I have succeeded in putting bounding boxes on more or less each letter, but am unable to put bounding boxes on each word. I am using the code below for the same



im_ns = cv.imread('image.jpg')
gray = cv.cvtColor(im_ns,cv.COLOR_BGR2GRAY)
blurred_g = cv.GaussianBlur(gray,(11,11),0)
ret, th1 = cv.threshold(blurred_g,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
th3_1 = cv.adaptiveThreshold(th3,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
plt.figure(figsize=(30,25))
plt.imshow(th3_1)
th3_er = cv.erode(th3_1,None,iterations=1)
th3_di = cv.dilate(th3_er,None,iterations=1)
labels = measure.label(th3_di1, neighbors=8, background=255)
mask = np.zeros(th3_di1.shape, dtype="uint8")
for lab in np.unique(labels):
if lab == 0:
continue

labelMask = np.zeros(th3_di.shape, dtype="uint8")
labelMask[labels == lab] = 255
numPixels = cv.countNonZero(labelMask)

if numPixels > 13:
mask = cv.add(mask, labelMask)

cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = contours.sort_contours(cnts)[0]

t = th3_di.copy()
for (i, c) in enumerate(cnts):
(x,y,w,h) = cv.boundingRect(c)
cv.rectangle(t,(x,y),(x+w,y+h),(0,255),2)

cv.imshow("Image", t)
cv.waitKey(10000)
cv.destroyAllWindows()


The output of the above code can be seen in the link below
Image with bounding box










share|improve this question













I am trying to put bounding boxes on each word which are there in a document. I have succeeded in putting bounding boxes on more or less each letter, but am unable to put bounding boxes on each word. I am using the code below for the same



im_ns = cv.imread('image.jpg')
gray = cv.cvtColor(im_ns,cv.COLOR_BGR2GRAY)
blurred_g = cv.GaussianBlur(gray,(11,11),0)
ret, th1 = cv.threshold(blurred_g,127,255,cv.THRESH_BINARY)
th2 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_MEAN_C,cv.THRESH_BINARY,11,2)
th3 = cv.adaptiveThreshold(blurred_g,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
th3_1 = cv.adaptiveThreshold(th3,255,cv.ADAPTIVE_THRESH_GAUSSIAN_C,cv.THRESH_BINARY,11,2)
plt.figure(figsize=(30,25))
plt.imshow(th3_1)
th3_er = cv.erode(th3_1,None,iterations=1)
th3_di = cv.dilate(th3_er,None,iterations=1)
labels = measure.label(th3_di1, neighbors=8, background=255)
mask = np.zeros(th3_di1.shape, dtype="uint8")
for lab in np.unique(labels):
if lab == 0:
continue

labelMask = np.zeros(th3_di.shape, dtype="uint8")
labelMask[labels == lab] = 255
numPixels = cv.countNonZero(labelMask)

if numPixels > 13:
mask = cv.add(mask, labelMask)

cnts = cv.findContours(mask.copy(), cv.RETR_EXTERNAL,cv.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
cnts = contours.sort_contours(cnts)[0]

t = th3_di.copy()
for (i, c) in enumerate(cnts):
(x,y,w,h) = cv.boundingRect(c)
cv.rectangle(t,(x,y),(x+w,y+h),(0,255),2)

cv.imshow("Image", t)
cv.waitKey(10000)
cv.destroyAllWindows()


The output of the above code can be seen in the link below
Image with bounding box







opencv image-processing image-segmentation






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 16:07









Arindam Bose

283




283












  • Maybe you can do morphological dilation to merge the letters using a small structure element. Then the connected components are words. Since there is blank space between words, it's less likely that different words are merged.
    – Kaiwen Chang
    Nov 23 at 13:47


















  • Maybe you can do morphological dilation to merge the letters using a small structure element. Then the connected components are words. Since there is blank space between words, it's less likely that different words are merged.
    – Kaiwen Chang
    Nov 23 at 13:47
















Maybe you can do morphological dilation to merge the letters using a small structure element. Then the connected components are words. Since there is blank space between words, it's less likely that different words are merged.
– Kaiwen Chang
Nov 23 at 13:47




Maybe you can do morphological dilation to merge the letters using a small structure element. Then the connected components are words. Since there is blank space between words, it's less likely that different words are merged.
– Kaiwen Chang
Nov 23 at 13:47

















active

oldest

votes











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',
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%2f53434729%2fgetting-bounding-boxes-on-words-instead-of-letters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • 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%2f53434729%2fgetting-bounding-boxes-on-words-instead-of-letters%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

What visual should I use to simply compare current year value vs last year in Power BI desktop

Alexandru Averescu

Trompette piccolo