1/14/2018

Histogram Equalization, Stretching, origin histo compare and example source code

Example source code for histogram equalization
and compare with origin histogram and stretching histogram.

result is like that:

origin image & histogram




stretching image and histogram






Equalization image and histogram
 

<gist>

</gist>


tags:
equalizeHist, cvtColor, normalize, calcHist

1/12/2018

python list, numpy slicing

Oh.. I seem to be old.. I need a memo everything..
This is memo for me about list slicing.

nums = list(range(5))
print(nums)      #[0, 1, 2, 3, 4]
print(nums[2:4]) #[2, 3]
print(nums[2:])  #[2, 3, 4] 
print(nums[:2])  #[0, 1]
print(nums[:])   #[0, 1, 2, 3, 4]
print(nums[:-1]) #[0, 1, 2, 3]
nums[2:4] = [8,9]
print(nums)      #[0, 1, 8, 9, 4]

result
[0, 1, 2, 3, 4]
[2, 3]
[2, 3, 4]
[0, 1]
[0, 1, 2, 3, 4]
[0, 1, 2, 3]
[0, 1, 8, 9, 4]


numpy slicing
import numpy as np

a = np.array([1, 2, 3, 4, 5])
print(a[1:3]) #array [2 3]
print(a[-1]) #5
a[0:2] = 9
print(a) #array [9 9 3 4 5]
b = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
print(b)
#array
#[[ 1  2  3  4]
# [ 5  6  7  8]
# [ 9 10 11 12]]

print(b[:,1]) #array [2 6 10]
print(b[-1])      #array [9 10 11 12]
print(b[-1,:])    #array [9 10 11 12]
print(b[-1, ...]) #array [9 10 11 12]

print(b[0:2, :])
#array#[[1 2 3 4]# [5 6 7 8]]
😀

12/31/2017

To make virtualenv using mkvirtualenv or virtualenv, simple command memo.

I forget this command every time..
so, this is my memo
mkvirtualenv env-py2 -p python2 #(or python3)
workon env-py2
deactivate

but what is different with
sudo pip install virtualenv
virtualenv env
sourse env/bin/activate
source deactivate
workon is easier then source .... , isn't it?


etc.
rmvirtualenv name..

12/30/2017

opencv histogram stretching, example source code.

Histogram stretching
It's just adjusting the range with the same ratio.

For example, there is a range of numbers.
60, 61, 62, 63, 64, 65
Stretching is to extent other range, such as 0~255.
So, if we stitching 60~65 to 0~255, numbers will be like that.
60 -> 0
61 -> 51
62 -> 102
63 -> 153
64 -> 204
65 -> 255

so, in case of histogram, origin histogram will be stretched like that

image source : https://stackoverflow.com/questions/41118808/difference-between-contrast-stretching-and-histogram-equalization

then, let's look at code and result with real image.

origin input image


histogram


stretched image


histogram of stretched image


source code
<gist start>

<gist end>




Django + install rich html text editor (WYSIWYG, ckeditor)


I recommend you do this on virtual-env.

1. install packages

pip install django-ckeditor
pip install image (optional)

2. Set settings.py

Add ckeditor in INSTALLED_APPS
INSTALLED_APPS = [ 'django.contrib.admin',                   
                   'django.contrib.auth',                   
                    ... 
                   'ckeditor',]

Add this contents end of settings.py
CKEDITOR_UPLOAD_PATH = "uploads/" 
CKEDITOR_CONFIGS = { 
    'default': { 'toolbar': None, },
}

3. Set url.py

Add "url(r'^ckeditor/', include('ckeditor_uploader.urls'))," in urlpatterns
urlpatterns = [ url(r'^admin/', admin.site.urls),                
               ... 
                url(r'^ckeditor/', include('ckeditor_uploader.urls')),                
]

4. set models.py

Add import RichTextField
change "models.TextField()" to "RichTextField()"

from ckeditor.fields import RichTextField 
# Create your models here 
class OpenCV_Post(models.Model): 
    author = models.ForeignKey('auth.User') 
    title = models.CharField(max_length=200)
    # text = models.TextField()    
    text = RichTextField()

5. migration!

>>python manage.py makemigrations
>>python manage.py migrate


The Result on Admin






reference
https://www.youtube.com/watch?v=W8PTD7SszDI

12/28/2017

Hue histogram example opencv source code

Hue histogram example source code.

input


Hue histogram output


source code
<gist start>

<gist end>

calcHist for RGB image, opencv histogram example

A Example source code for rgb histogram, the source code uses calcHist function in opencv.

Important things in the source code are the part of split rgb mat to vector[3] and drawing part.
Read the code carefully, so then you can understand easliy. ^^

input


the result of rgb histogram


<gist code start>

<gist code end>



reference : https://docs.opencv.org/2.4/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html