Increase image brightness without overflow












8














I got a problem when trying to increase image brightness.



Here is the origin image:



enter image description here



The image I wanted to get is like this:



enter image description here



Now to increase the brightness with the following code:



    image = cv2.imread("/home/wni/vbshare/tmp/a4_index2.png",0)

if sum(image[0])/len(image[0])<200:
new = np.where((255-image)<image,255,image*2)
else:
new = image
return new


And, I got the following image:



enter image description here



So, seems brightness of some points overflowed.



And I tried to change the threshold from 200 to some other number, e.g. 125, 100, 140 .etc
However, the image brightness stays either almost same dark or overflow.



Env:



Python: 2.7.10



Opencv: 3.2.0



Any suggestion for this is appreciated.



Thanks.










share|improve this question
























  • You may use cv2.adaptiveThreshold() to get somewhat similar results but you may not be able to get the exact original form.
    – ZdaR
    May 18 '17 at 13:43










  • None of the attached images work.
    – Dan Mašek
    May 18 '17 at 16:00










  • You sure? It's OK for me
    – Wesley
    May 18 '17 at 16:02










  • @Wesley Interesting. Must have been some temporary issue.
    – Dan Mašek
    May 18 '17 at 16:05










  • Anyway, to your problem -- to avoid the overflow I'd use an operation that saturates. e.g. cv2.add the source array to itself, using a mask to select which elements to update. That's not to say it's an ideal approach.
    – Dan Mašek
    May 18 '17 at 16:08


















8














I got a problem when trying to increase image brightness.



Here is the origin image:



enter image description here



The image I wanted to get is like this:



enter image description here



Now to increase the brightness with the following code:



    image = cv2.imread("/home/wni/vbshare/tmp/a4_index2.png",0)

if sum(image[0])/len(image[0])<200:
new = np.where((255-image)<image,255,image*2)
else:
new = image
return new


And, I got the following image:



enter image description here



So, seems brightness of some points overflowed.



And I tried to change the threshold from 200 to some other number, e.g. 125, 100, 140 .etc
However, the image brightness stays either almost same dark or overflow.



Env:



Python: 2.7.10



Opencv: 3.2.0



Any suggestion for this is appreciated.



Thanks.










share|improve this question
























  • You may use cv2.adaptiveThreshold() to get somewhat similar results but you may not be able to get the exact original form.
    – ZdaR
    May 18 '17 at 13:43










  • None of the attached images work.
    – Dan Mašek
    May 18 '17 at 16:00










  • You sure? It's OK for me
    – Wesley
    May 18 '17 at 16:02










  • @Wesley Interesting. Must have been some temporary issue.
    – Dan Mašek
    May 18 '17 at 16:05










  • Anyway, to your problem -- to avoid the overflow I'd use an operation that saturates. e.g. cv2.add the source array to itself, using a mask to select which elements to update. That's not to say it's an ideal approach.
    – Dan Mašek
    May 18 '17 at 16:08
















8












8








8


10





I got a problem when trying to increase image brightness.



Here is the origin image:



enter image description here



The image I wanted to get is like this:



enter image description here



Now to increase the brightness with the following code:



    image = cv2.imread("/home/wni/vbshare/tmp/a4_index2.png",0)

if sum(image[0])/len(image[0])<200:
new = np.where((255-image)<image,255,image*2)
else:
new = image
return new


And, I got the following image:



enter image description here



So, seems brightness of some points overflowed.



And I tried to change the threshold from 200 to some other number, e.g. 125, 100, 140 .etc
However, the image brightness stays either almost same dark or overflow.



Env:



Python: 2.7.10



Opencv: 3.2.0



Any suggestion for this is appreciated.



Thanks.










share|improve this question















I got a problem when trying to increase image brightness.



Here is the origin image:



enter image description here



The image I wanted to get is like this:



enter image description here



Now to increase the brightness with the following code:



    image = cv2.imread("/home/wni/vbshare/tmp/a4_index2.png",0)

if sum(image[0])/len(image[0])<200:
new = np.where((255-image)<image,255,image*2)
else:
new = image
return new


And, I got the following image:



enter image description here



So, seems brightness of some points overflowed.



And I tried to change the threshold from 200 to some other number, e.g. 125, 100, 140 .etc
However, the image brightness stays either almost same dark or overflow.



Env:



Python: 2.7.10



Opencv: 3.2.0



Any suggestion for this is appreciated.



Thanks.







python numpy opencv image-processing






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 23:17









Dan Mašek

8,73532446




8,73532446










asked May 18 '17 at 12:30









Wesley

681519




681519












  • You may use cv2.adaptiveThreshold() to get somewhat similar results but you may not be able to get the exact original form.
    – ZdaR
    May 18 '17 at 13:43










  • None of the attached images work.
    – Dan Mašek
    May 18 '17 at 16:00










  • You sure? It's OK for me
    – Wesley
    May 18 '17 at 16:02










  • @Wesley Interesting. Must have been some temporary issue.
    – Dan Mašek
    May 18 '17 at 16:05










  • Anyway, to your problem -- to avoid the overflow I'd use an operation that saturates. e.g. cv2.add the source array to itself, using a mask to select which elements to update. That's not to say it's an ideal approach.
    – Dan Mašek
    May 18 '17 at 16:08




















  • You may use cv2.adaptiveThreshold() to get somewhat similar results but you may not be able to get the exact original form.
    – ZdaR
    May 18 '17 at 13:43










  • None of the attached images work.
    – Dan Mašek
    May 18 '17 at 16:00










  • You sure? It's OK for me
    – Wesley
    May 18 '17 at 16:02










  • @Wesley Interesting. Must have been some temporary issue.
    – Dan Mašek
    May 18 '17 at 16:05










  • Anyway, to your problem -- to avoid the overflow I'd use an operation that saturates. e.g. cv2.add the source array to itself, using a mask to select which elements to update. That's not to say it's an ideal approach.
    – Dan Mašek
    May 18 '17 at 16:08


















You may use cv2.adaptiveThreshold() to get somewhat similar results but you may not be able to get the exact original form.
– ZdaR
May 18 '17 at 13:43




You may use cv2.adaptiveThreshold() to get somewhat similar results but you may not be able to get the exact original form.
– ZdaR
May 18 '17 at 13:43












None of the attached images work.
– Dan Mašek
May 18 '17 at 16:00




None of the attached images work.
– Dan Mašek
May 18 '17 at 16:00












You sure? It's OK for me
– Wesley
May 18 '17 at 16:02




You sure? It's OK for me
– Wesley
May 18 '17 at 16:02












@Wesley Interesting. Must have been some temporary issue.
– Dan Mašek
May 18 '17 at 16:05




@Wesley Interesting. Must have been some temporary issue.
– Dan Mašek
May 18 '17 at 16:05












Anyway, to your problem -- to avoid the overflow I'd use an operation that saturates. e.g. cv2.add the source array to itself, using a mask to select which elements to update. That's not to say it's an ideal approach.
– Dan Mašek
May 18 '17 at 16:08






Anyway, to your problem -- to avoid the overflow I'd use an operation that saturates. e.g. cv2.add the source array to itself, using a mask to select which elements to update. That's not to say it's an ideal approach.
– Dan Mašek
May 18 '17 at 16:08














2 Answers
2






active

oldest

votes


















18














Here's my shot at a simple algorithm for cleaning up that particular image. Feel free to play with it and tweak it further to get the desired result.



NB: The code shown should work both with the 2.4.x and 3.x branches of OpenCV.



Step 0



Load the input image as grayscale.



img = cv2.imread('paper.jpg',0)


Step 1



Dilate the image, in order to get rid of the text.
This step somewhat helps to preserve the bar code.



dilated_img = cv2.dilate(img, np.ones((7,7), np.uint8)) 


Dilated



Step 2



Median blur the result with a decent sized kernel to further suppress any text.



This should get us a fairly good background image that contains all the shadows and/or discoloration.



bg_img = cv2.medianBlur(dilated_img, 21)


Blurred



Step 3



Calculate the difference between the original and the background we just obtained. The bits that are identical will be black (close to 0 difference), the text will be white (large difference).



Since we want black on white, we invert the result.



diff_img = 255 - cv2.absdiff(img, bg_img)


Inverted Difference



Step 4



Normalize the image, so that we use the full dynamic range.



norm_img = diff_img.copy() # Needed for 3.x compatibility
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Normalized



Step 5



At this point we still have the paper somewhat gray. We can truncate that away, and re-normalize the image.



_, thr_img = cv2.threshold(norm_img, 230, 0, cv2.THRESH_TRUNC)
cv2.normalize(thr_img, thr_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Gray Trimmed



Done...



Well, at least for me ;) You will probably want to crop it, and do whatever other post-processing you desire.





Note: It might be worth switching to higher precision (16+ bit int or float) after you get the difference image, in order to minimize accumulating rounding errors in the repeated normalizations.






share|improve this answer























  • Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
    – Wesley
    May 23 '17 at 8:45



















2














What you need is thresholding. This will allow you to set everything that isn't very dark to pure white, and set the very dark stuff to straight black (or any other colors). This should get you started:



cutoff_val = 100 # everything above this is set to set_color
set_color = 255
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_BINARY)


After you play around with that, you can get better results using adaptive thresholding.



See this great tutorial doing basically what you want---and bonus, it includes a tutorial on warping to get a rectangular region for the paper, too!



Edit: I got pretty good results with your image combining thresholding and then adaptive thresholding.



cutoff_val = 150 # everything above this is set to the cutoff val
set_color = 255 # if
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_TRUNC)
window_sz = 3
thresh_img2 = cv2.adaptiveThreshold(thresh_img,set_color,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,window_sz,2)





share|improve this answer























  • Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
    – Wesley
    May 18 '17 at 16:01











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%2f44047819%2fincrease-image-brightness-without-overflow%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









18














Here's my shot at a simple algorithm for cleaning up that particular image. Feel free to play with it and tweak it further to get the desired result.



NB: The code shown should work both with the 2.4.x and 3.x branches of OpenCV.



Step 0



Load the input image as grayscale.



img = cv2.imread('paper.jpg',0)


Step 1



Dilate the image, in order to get rid of the text.
This step somewhat helps to preserve the bar code.



dilated_img = cv2.dilate(img, np.ones((7,7), np.uint8)) 


Dilated



Step 2



Median blur the result with a decent sized kernel to further suppress any text.



This should get us a fairly good background image that contains all the shadows and/or discoloration.



bg_img = cv2.medianBlur(dilated_img, 21)


Blurred



Step 3



Calculate the difference between the original and the background we just obtained. The bits that are identical will be black (close to 0 difference), the text will be white (large difference).



Since we want black on white, we invert the result.



diff_img = 255 - cv2.absdiff(img, bg_img)


Inverted Difference



Step 4



Normalize the image, so that we use the full dynamic range.



norm_img = diff_img.copy() # Needed for 3.x compatibility
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Normalized



Step 5



At this point we still have the paper somewhat gray. We can truncate that away, and re-normalize the image.



_, thr_img = cv2.threshold(norm_img, 230, 0, cv2.THRESH_TRUNC)
cv2.normalize(thr_img, thr_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Gray Trimmed



Done...



Well, at least for me ;) You will probably want to crop it, and do whatever other post-processing you desire.





Note: It might be worth switching to higher precision (16+ bit int or float) after you get the difference image, in order to minimize accumulating rounding errors in the repeated normalizations.






share|improve this answer























  • Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
    – Wesley
    May 23 '17 at 8:45
















18














Here's my shot at a simple algorithm for cleaning up that particular image. Feel free to play with it and tweak it further to get the desired result.



NB: The code shown should work both with the 2.4.x and 3.x branches of OpenCV.



Step 0



Load the input image as grayscale.



img = cv2.imread('paper.jpg',0)


Step 1



Dilate the image, in order to get rid of the text.
This step somewhat helps to preserve the bar code.



dilated_img = cv2.dilate(img, np.ones((7,7), np.uint8)) 


Dilated



Step 2



Median blur the result with a decent sized kernel to further suppress any text.



This should get us a fairly good background image that contains all the shadows and/or discoloration.



bg_img = cv2.medianBlur(dilated_img, 21)


Blurred



Step 3



Calculate the difference between the original and the background we just obtained. The bits that are identical will be black (close to 0 difference), the text will be white (large difference).



Since we want black on white, we invert the result.



diff_img = 255 - cv2.absdiff(img, bg_img)


Inverted Difference



Step 4



Normalize the image, so that we use the full dynamic range.



norm_img = diff_img.copy() # Needed for 3.x compatibility
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Normalized



Step 5



At this point we still have the paper somewhat gray. We can truncate that away, and re-normalize the image.



_, thr_img = cv2.threshold(norm_img, 230, 0, cv2.THRESH_TRUNC)
cv2.normalize(thr_img, thr_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Gray Trimmed



Done...



Well, at least for me ;) You will probably want to crop it, and do whatever other post-processing you desire.





Note: It might be worth switching to higher precision (16+ bit int or float) after you get the difference image, in order to minimize accumulating rounding errors in the repeated normalizations.






share|improve this answer























  • Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
    – Wesley
    May 23 '17 at 8:45














18












18








18






Here's my shot at a simple algorithm for cleaning up that particular image. Feel free to play with it and tweak it further to get the desired result.



NB: The code shown should work both with the 2.4.x and 3.x branches of OpenCV.



Step 0



Load the input image as grayscale.



img = cv2.imread('paper.jpg',0)


Step 1



Dilate the image, in order to get rid of the text.
This step somewhat helps to preserve the bar code.



dilated_img = cv2.dilate(img, np.ones((7,7), np.uint8)) 


Dilated



Step 2



Median blur the result with a decent sized kernel to further suppress any text.



This should get us a fairly good background image that contains all the shadows and/or discoloration.



bg_img = cv2.medianBlur(dilated_img, 21)


Blurred



Step 3



Calculate the difference between the original and the background we just obtained. The bits that are identical will be black (close to 0 difference), the text will be white (large difference).



Since we want black on white, we invert the result.



diff_img = 255 - cv2.absdiff(img, bg_img)


Inverted Difference



Step 4



Normalize the image, so that we use the full dynamic range.



norm_img = diff_img.copy() # Needed for 3.x compatibility
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Normalized



Step 5



At this point we still have the paper somewhat gray. We can truncate that away, and re-normalize the image.



_, thr_img = cv2.threshold(norm_img, 230, 0, cv2.THRESH_TRUNC)
cv2.normalize(thr_img, thr_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Gray Trimmed



Done...



Well, at least for me ;) You will probably want to crop it, and do whatever other post-processing you desire.





Note: It might be worth switching to higher precision (16+ bit int or float) after you get the difference image, in order to minimize accumulating rounding errors in the repeated normalizations.






share|improve this answer














Here's my shot at a simple algorithm for cleaning up that particular image. Feel free to play with it and tweak it further to get the desired result.



NB: The code shown should work both with the 2.4.x and 3.x branches of OpenCV.



Step 0



Load the input image as grayscale.



img = cv2.imread('paper.jpg',0)


Step 1



Dilate the image, in order to get rid of the text.
This step somewhat helps to preserve the bar code.



dilated_img = cv2.dilate(img, np.ones((7,7), np.uint8)) 


Dilated



Step 2



Median blur the result with a decent sized kernel to further suppress any text.



This should get us a fairly good background image that contains all the shadows and/or discoloration.



bg_img = cv2.medianBlur(dilated_img, 21)


Blurred



Step 3



Calculate the difference between the original and the background we just obtained. The bits that are identical will be black (close to 0 difference), the text will be white (large difference).



Since we want black on white, we invert the result.



diff_img = 255 - cv2.absdiff(img, bg_img)


Inverted Difference



Step 4



Normalize the image, so that we use the full dynamic range.



norm_img = diff_img.copy() # Needed for 3.x compatibility
cv2.normalize(diff_img, norm_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Normalized



Step 5



At this point we still have the paper somewhat gray. We can truncate that away, and re-normalize the image.



_, thr_img = cv2.threshold(norm_img, 230, 0, cv2.THRESH_TRUNC)
cv2.normalize(thr_img, thr_img, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8UC1)


Gray Trimmed



Done...



Well, at least for me ;) You will probably want to crop it, and do whatever other post-processing you desire.





Note: It might be worth switching to higher precision (16+ bit int or float) after you get the difference image, in order to minimize accumulating rounding errors in the repeated normalizations.







share|improve this answer














share|improve this answer



share|improve this answer








edited May 19 '17 at 0:11

























answered May 18 '17 at 17:48









Dan Mašek

8,73532446




8,73532446












  • Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
    – Wesley
    May 23 '17 at 8:45


















  • Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
    – Wesley
    May 23 '17 at 8:45
















Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
– Wesley
May 23 '17 at 8:45




Hi, thanks for your response. I just have a try and get what I wanna. Will accept as answer.
– Wesley
May 23 '17 at 8:45













2














What you need is thresholding. This will allow you to set everything that isn't very dark to pure white, and set the very dark stuff to straight black (or any other colors). This should get you started:



cutoff_val = 100 # everything above this is set to set_color
set_color = 255
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_BINARY)


After you play around with that, you can get better results using adaptive thresholding.



See this great tutorial doing basically what you want---and bonus, it includes a tutorial on warping to get a rectangular region for the paper, too!



Edit: I got pretty good results with your image combining thresholding and then adaptive thresholding.



cutoff_val = 150 # everything above this is set to the cutoff val
set_color = 255 # if
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_TRUNC)
window_sz = 3
thresh_img2 = cv2.adaptiveThreshold(thresh_img,set_color,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,window_sz,2)





share|improve this answer























  • Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
    – Wesley
    May 18 '17 at 16:01
















2














What you need is thresholding. This will allow you to set everything that isn't very dark to pure white, and set the very dark stuff to straight black (or any other colors). This should get you started:



cutoff_val = 100 # everything above this is set to set_color
set_color = 255
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_BINARY)


After you play around with that, you can get better results using adaptive thresholding.



See this great tutorial doing basically what you want---and bonus, it includes a tutorial on warping to get a rectangular region for the paper, too!



Edit: I got pretty good results with your image combining thresholding and then adaptive thresholding.



cutoff_val = 150 # everything above this is set to the cutoff val
set_color = 255 # if
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_TRUNC)
window_sz = 3
thresh_img2 = cv2.adaptiveThreshold(thresh_img,set_color,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,window_sz,2)





share|improve this answer























  • Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
    – Wesley
    May 18 '17 at 16:01














2












2








2






What you need is thresholding. This will allow you to set everything that isn't very dark to pure white, and set the very dark stuff to straight black (or any other colors). This should get you started:



cutoff_val = 100 # everything above this is set to set_color
set_color = 255
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_BINARY)


After you play around with that, you can get better results using adaptive thresholding.



See this great tutorial doing basically what you want---and bonus, it includes a tutorial on warping to get a rectangular region for the paper, too!



Edit: I got pretty good results with your image combining thresholding and then adaptive thresholding.



cutoff_val = 150 # everything above this is set to the cutoff val
set_color = 255 # if
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_TRUNC)
window_sz = 3
thresh_img2 = cv2.adaptiveThreshold(thresh_img,set_color,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,window_sz,2)





share|improve this answer














What you need is thresholding. This will allow you to set everything that isn't very dark to pure white, and set the very dark stuff to straight black (or any other colors). This should get you started:



cutoff_val = 100 # everything above this is set to set_color
set_color = 255
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_BINARY)


After you play around with that, you can get better results using adaptive thresholding.



See this great tutorial doing basically what you want---and bonus, it includes a tutorial on warping to get a rectangular region for the paper, too!



Edit: I got pretty good results with your image combining thresholding and then adaptive thresholding.



cutoff_val = 150 # everything above this is set to the cutoff val
set_color = 255 # if
ret,thresh_img = cv2.threshold(image,cutoff_val,set_color,cv2.THRESH_TRUNC)
window_sz = 3
thresh_img2 = cv2.adaptiveThreshold(thresh_img,set_color,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,window_sz,2)






share|improve this answer














share|improve this answer



share|improve this answer








edited May 18 '17 at 14:20

























answered May 18 '17 at 13:51









Alexander Reynolds

8,97511639




8,97511639












  • Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
    – Wesley
    May 18 '17 at 16:01


















  • Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
    – Wesley
    May 18 '17 at 16:01
















Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
– Wesley
May 18 '17 at 16:01




Hi, I just have a try on your code, yeah, It get brighter, but also be glurry, I guess to be from cv2.ADAPTIVE_THRESH_GAUSSIAN_C, so I changed to cv2.ADAPTIVE_THRESH_MEAN_C, but with almost same result. Because I still need to do some ocr recognize after this, so, could you have suggestion on keep clarity when increasing brightness :-)
– Wesley
May 18 '17 at 16:01


















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%2f44047819%2fincrease-image-brightness-without-overflow%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

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'