<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: haratena9</title>
    <description>The latest articles on DEV Community by haratena9 (@haratena9).</description>
    <link>https://dev.to/haratena9</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F813880%2F621b117f-dec4-4eb2-8cc9-8686ac71b4d4.png</url>
      <title>DEV Community: haratena9</title>
      <link>https://dev.to/haratena9</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/haratena9"/>
    <language>en</language>
    <item>
      <title>Image Processing #3 Statistics</title>
      <dc:creator>haratena9</dc:creator>
      <pubDate>Mon, 14 Feb 2022 13:31:17 +0000</pubDate>
      <link>https://dev.to/haratena9/image-processing-3-statistics-340e</link>
      <guid>https://dev.to/haratena9/image-processing-3-statistics-340e</guid>
      <description>&lt;h2&gt;
  
  
  About this memorandum
&lt;/h2&gt;

&lt;p&gt;Recently, I had a chance to experience deep learning and image/video processing at work, but there were many things I didn't understand about how to touch parameters and amplify data, so I decided to study video processing from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Statistics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Image (pixel value) statistics: used for various image analysis

&lt;ul&gt;
&lt;li&gt;Mean value&lt;/li&gt;
&lt;li&gt;Median&lt;/li&gt;
&lt;li&gt;Frequent value&lt;/li&gt;
&lt;li&gt;Variance&lt;/li&gt;
&lt;li&gt;Contrast&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Some can be calculated from histograms&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;The right image shows a white whiteboard with a black metamon on it.&lt;br&gt;
max:1 (white), min:0.0 (black), and we can see that the contrast is 1.0.&lt;br&gt;
There are many ways to calculate contrast.(see code below)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finnv8vktwr4781f8u1ue.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Finnv8vktwr4781f8u1ue.png" alt="Summary"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Statistics calculation code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im_files =[file_path_A, file_path_B, file_path_C]

for file in im_files:
    im = imread(file)[:,:,:3]  # For RGBA, extract only RGB
    im = rgb2gray(im)
    imshow(im)
    plt.show()

    print('mean: ', im.mean())
    print('std: ', im.std())
    print('median: ', np.median(im))
    print('max: ', im.max())
    print('min: ', im.min())
    print('contrast1: ', (im.max() - im.min()) / (im.max() + im.min()) ) # Michelson contrast
    print('contrast2: ', im.max() / im.min() if im.min() &amp;gt; 0 else np.nan ) # contrast ratio
    print('contrast3: ', im.max() - im.min() ) # contrast difference
    print()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Calculate the mean and variance
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Unsuggested code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Keep the definition formula&lt;/li&gt;
&lt;li&gt;Large computational complexity: 2 double loops
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnt43gizghytem6qaixhs.png" alt="fomula"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im = rgb2gray(imread('file_path'))
h, w = im.shape

mean = 0
for y in range(h):
    for x in range(w):
        mean += im[y, x]
mean /= h * w
print('mean: ', mean)

var = 0
for y in range(h):
    for x in range(w):
        var += (im[y, x] - mean)**2
var /= h * w
print('variance: ', var)
print('std: ', np.sqrt(var))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Suggested code
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Transformed formula&lt;/li&gt;
&lt;li&gt;Half the computation: one loop.
&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frp4k2xq9r7l31ccb7jf6.png" alt="Transformed formula"&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im = rgb2gray(imread('file_path'))
h, w = im.shape

mean = 0
var = 0
for y in range(h):
    for x in range(w):
        mean += im[y, x]
        var  += im[y, x]**2

mean /= h * w
print('mean: ', mean)

var /= h * w
var -= mean**2
print('variance: ', var)
print('std: ', np.sqrt(var))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>imageprocessing</category>
      <category>formyownuse</category>
    </item>
    <item>
      <title>Image Processing #2 RGB, Histogram</title>
      <dc:creator>haratena9</dc:creator>
      <pubDate>Sat, 12 Feb 2022 06:18:15 +0000</pubDate>
      <link>https://dev.to/haratena9/image-processing-2-rgb-histogram-5dii</link>
      <guid>https://dev.to/haratena9/image-processing-2-rgb-histogram-5dii</guid>
      <description>&lt;h2&gt;
  
  
  About this memorandum
&lt;/h2&gt;

&lt;p&gt;Recently, I had a chance to experience deep learning and image/video processing at work, but there were many things I didn't understand about how to touch parameters and amplify data, so I decided to study video processing from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Color images and grayscale images
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Grayscale image: pixel value represents brightness&lt;/li&gt;
&lt;li&gt;RGB color image: the pixel value represents the brightness of each RGB (for clarity, see the image below)

&lt;ul&gt;
&lt;li&gt;1 pixel: 8bit×3=24bit(3Byte)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxy267jb90op0owdbry8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzxy267jb90op0owdbry8.png" alt="Summary"&gt;&lt;/a&gt;&lt;br&gt;
However, in reality, the &lt;strong&gt;correct understanding&lt;/strong&gt; is that it consists of three R, G and B grayscale images.&lt;br&gt;
Since the car is originally red, the pixel value in the R channel on the left is close to 255, and it appears to be white in the grayscale image. On the other hand, the G and B channels have smaller pixel values and appear to be black. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96ndolyegxckt0mb6hiw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F96ndolyegxckt0mb6hiw.png" alt="Original car"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbaagedeww5yttj6yhn8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffbaagedeww5yttj6yhn8.png" alt="reality"&gt;&lt;/a&gt;&lt;br&gt;
Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im = imread([file_path])

imshow(im)
plt.title("original RGB image")
plt.show()

r_channel = im[:, :, 0]
g_channel = im[:, :, 1]
b_channel = im[:, :, 2]

fig = plt.figure(figsize=(15,3))

for i, c in zip(range(3), 'RGB'):
    ax = fig.add_subplot(1, 3, i + 1)
    imshow(im[:, :, i], vmin=0, vmax=255)
    plt.colorbar()
    plt.title(f'{c} channel')

plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  RGB&amp;amp;BGR
&lt;/h2&gt;

&lt;p&gt;The only difference is the interpretation of the data, but be careful.&lt;/p&gt;

&lt;h3&gt;
  
  
  RGB
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Used in many textbook descriptions.&lt;/li&gt;
&lt;li&gt;Many image processing libraries also use this format.

&lt;ul&gt;
&lt;li&gt;skimage, matplotlib for python &lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  BGR
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Also often used

&lt;ul&gt;
&lt;li&gt;opencv (python, C/C++)&lt;/li&gt;
&lt;li&gt;COLORREF on Windows (0x00bbggrr in hexadecimal)&lt;/li&gt;
&lt;li&gt;Hardware&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;A common mistake is that when images loaded with opencv (BGR) are displayed with scikit-image (RGB), the red and blue are reversed.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdac265zb73oz0i9zusy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwdac265zb73oz0i9zusy.png" alt="mistake "&gt;&lt;/a&gt;&lt;br&gt;
Code (example of mistake)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im_BGR = cv2.imread(INPUT_DIR + 'IMG-4034.JPG') # OpenCV
imshow(im_BGR) # matplotlibのimshowはRGBを仮定
plt.title('show BGR image as RGB image')
plt.axis('off')
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are several ways to fix this, including the following&lt;br&gt;
Code:RGB and GBR conversion&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;### use the built-in functions
im_BGR_to_RGB = cv2.cvtColor(im_BGR, cv2.COLOR_BGR2RGB)
imshow(im_BGR_to_RGB)
plt.title('show RGB-converted BGR image as RGB image')
plt.axis('off')
plt.show()

### not use the built-in functions1
im_BGR_to_RGB = im_BGR[:, :, ::-1]
imshow(im_BGR_to_RGB)
plt.title('show RGB-converted BGR image as RGB image')
plt.axis('off')
plt.show()

### not use the built-in functions1(explanation process of 1 above.)
im_BGR_to_RGB = np.zeros_like(im_BGR)

im_BGR_to_RGB[:, :, 0] = im_BGR[:, :, 2]
im_BGR_to_RGB[:, :, 1] = im_BGR[:, :, 1]
im_BGR_to_RGB[:, :, 2] = im_BGR[:, :, 0]

imshow(im_BGR_to_RGB)
plt.title('show RGB-converted BGR image as RGB image')
plt.axis('off')
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How to create a grayscale image.
&lt;/h2&gt;

&lt;p&gt;There is no wrong or right way to do this, just different standards.&lt;br&gt;
However, all methods look almost the same, but the values are different, so it is necessary to &lt;strong&gt;recognize&lt;/strong&gt; when working together.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fretxplcni8cf0v09s8mc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fretxplcni8cf0v09s8mc.png" alt="Original"&gt;&lt;/a&gt; &lt;br&gt;
use the built-in functions&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe218p8eylpwt2uqfmwji.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe218p8eylpwt2uqfmwji.png" alt="use the built-in functions"&gt;&lt;/a&gt;&lt;br&gt;
Divide the sum of the RGB values by three&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F19z7ww6qdgxnhmid7rv8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F19z7ww6qdgxnhmid7rv8.png" alt="Divide the sum of the RGB values by three"&gt;&lt;/a&gt;&lt;br&gt;
Standard: PAL/NTSC&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4o6iugjcanlldvl1bvvk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4o6iugjcanlldvl1bvvk.png" alt="PAL/NTSC"&gt;&lt;/a&gt;&lt;br&gt;
Standard: HDTV (same as built-in functions)&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1flhrw1uplwzve9l9yp.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs1flhrw1uplwzve9l9yp.png" alt="HDTV "&gt;&lt;/a&gt;&lt;br&gt;
Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im = imread(INPUT_DIR + 'IMG-4034.JPG')

imshow(im)
plt.title("original RGB image")
plt.show()

# Using the built-in rgb2gray function;gray = 0.2125 R + 0.7154 G + 0.0721 B
im_gray1 = rgb2gray(im)
imshow(im_gray1, vmin=0, vmax=1) # 型はfloat，範囲は[0,1]になる
plt.colorbar()
plt.title("rgb2gray min {0} max {1}".format(im_gray1.min(), im_gray1.max() ))
plt.show()

# The average of RGB is used as a grayscale image. First convert to float (the range will be [0,255]), then convert to uint8 for display.
im_gray2 = (im[:,:,0].astype(float) +
            im[:,:,1].astype(float) + 
            im[:,:,2].astype(float)) / 3
imshow(im_gray2, vmin=0, vmax=255)
plt.colorbar()
plt.title("(R+B+G)/3 min {0:.2f} max {1:.2f}".format(im_gray2.min(), im_gray2.max() ))
plt.show()


# The weighted average of RGB is used as the grayscale image.
# https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems
im_gray3 = (0.299 * im[:,:,0].astype(float) +
            0.587 * im[:,:,1].astype(float) + 
            0.114 * im[:,:,2].astype(float))
imshow(im_gray3, vmin=0, vmax=255)
plt.colorbar()
plt.title("$\gamma'$ of PAL and NTSC min {0:.2f} max {1:.2f}".format(im_gray3.min(), im_gray3.max() ))
plt.show()

# The weighted average of RGB is used as a grayscale image. The weight coefficients vary depending on the standard.
# https://en.wikipedia.org/wiki/Grayscale#Luma_coding_in_video_systems
# This is what rgb2gray() uses.http://scikit-image.org/docs/dev/api/skimage.color.html#skimage.color.rgb2gray
im_gray4 = (0.2126 * im[:,:,0].astype(float) +
            0.7152 * im[:,:,1].astype(float) + 
            0.0722 * im[:,:,2].astype(float))
imshow(im_gray4, vmin=0, vmax=255)
plt.colorbar()
plt.title("$\gamma'$ of HDTV min {0:.2f} max {1:.2f}".format(im_gray4.min(), im_gray4.max() ))
plt.show()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Histogram
&lt;/h2&gt;

&lt;p&gt;A graph showing the frequency distribution of pixel values.&lt;br&gt;
The third picture shows a drawing of Metamon on a whiteboard.&lt;br&gt;
The peak of the Metamon drawing can be seen around 0.5~0.8, although it is a little uneven depending on the amount of ink in the marker (the amount of force used when drawing, etc.).&lt;br&gt;
In addition, the area where the PC display on the other side of the whiteboard is reflected (to the left of Metamon's mouth) is quite white.This is thought to be the peak around 0.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5s2n5tb4fzhormbd4sl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd5s2n5tb4fzhormbd4sl.png" alt="Hist1"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6zhith66b880ywgugrb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr6zhith66b880ywgugrb.png" alt="Hist2"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F857ndug4gm9et95i6hbf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F857ndug4gm9et95i6hbf.png" alt="Hist3"&gt;&lt;/a&gt;&lt;br&gt;
Code&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;im_files = ['file_path1', 'file_path2', 'file_path3']

for file in im_files:
    im = imread(file)[:,:,:3]  # In the case of RGBA, extract only RGB

    fig = plt.figure(figsize=(20,3))

    ax = fig.add_subplot(1, 3, 1)
    im = rgb2gray(im) # Range;[0,1]
    imshow(im)
    plt.axis('off')

    bins = 256

    ax = fig.add_subplot(1, 3, 2)
    freq, bins = histogram(im)
    plt.plot(bins, freq)
    plt.xlabel("intensity")
    plt.ylabel("frequency")
    plt.title('histogram (linear)')
    plt.xlim(0,1)


    ax = fig.add_subplot(1, 3, 3)
    freq, bins = histogram(im)
    plt.plot(bins, freq)
    plt.xlabel("intensity")
    plt.ylabel("log frequency")
    plt.yscale('log')
    plt.title('histogram (log)')
    plt.xlim(0,1)

    plt.show();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>python</category>
      <category>imageprocessing</category>
      <category>formyownuse</category>
    </item>
    <item>
      <title>Image Processing #1 Pixels, Quantization, and Sampling</title>
      <dc:creator>haratena9</dc:creator>
      <pubDate>Sat, 12 Feb 2022 05:31:47 +0000</pubDate>
      <link>https://dev.to/haratena9/memorandum-image-processing-1-pixels-quantization-and-sampling-1n02</link>
      <guid>https://dev.to/haratena9/memorandum-image-processing-1-pixels-quantization-and-sampling-1n02</guid>
      <description>&lt;h2&gt;
  
  
  About this memorandum
&lt;/h2&gt;

&lt;p&gt;Recently, I had a chance to experience deep learning and image/video processing at work, but there were many things I didn't understand about how to touch parameters and amplify data, so I decided to study video processing from scratch.&lt;/p&gt;

&lt;h2&gt;
  
  
  Digital image (an image is a collection of pixels)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;An image is made up of pixels.&lt;/li&gt;
&lt;li&gt;Pixel value

&lt;ul&gt;
&lt;li&gt;unit8:an integer value from 0 to 255 (0:white =&amp;gt; black:255)&lt;/li&gt;
&lt;li&gt;float:real number between 0 and 1.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Pixel size

&lt;ul&gt;
&lt;li&gt;unit8:1byte/pixel (8bit) &lt;/li&gt;
&lt;li&gt;float32:4bytes/pixel (32bit)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Prayscale image

&lt;ul&gt;
&lt;li&gt;The pixel value represents only the brightness of the image.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Represented as a two-dimensional array in the program.&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

im = imread('[file path]')
imshow(im)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7l73yqupzo7n9j775o9t.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7l73yqupzo7n9j775o9t.png" alt="original image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

im_eye = im[70:100, 120:150]
imshow(im_eye)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flmjt6sucst49lkvvenyi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flmjt6sucst49lkvvenyi.png" alt="selected area1"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

im_eye2 = im[80:90, 125:140]
imshow(im_eye2)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fswovk0tyd9x705vhklix.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fswovk0tyd9x705vhklix.png" alt="selected area2"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  summary
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qnw7uk9lr4chkzy1ffa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3qnw7uk9lr4chkzy1ffa.png" alt="summary"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Grayscale image
&lt;/h2&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

im_gray = cv2.imread('[file_path]', cv2.IMREAD_GRAYSCALE)
imshow(im_gray)


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3srgf44moep0zc94wx2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fc3srgf44moep0zc94wx2.png" alt="Image Grayscale "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Notes:accessing the image array
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Order of access to arrays

&lt;ul&gt;
&lt;li&gt;Rows, columns&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Access order to pixels

&lt;ul&gt;
&lt;li&gt;Vertical, horizontal&lt;/li&gt;
&lt;li&gt;y, x&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;If looping, outside is y, inside is x

&lt;ul&gt;
&lt;li&gt;The second index in the array is the contiguous memory area.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;In other words, it is the opposite of the general sense (x, y), so it is a hotbed of bugs during implementation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Code&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

im = np.zeros((5, 5)) # image with width 3✕ height 2 (array)
im[2, 3] = 255 # Access pixels with (x,y)=(3,2)
print(im)

imshow(im)
plt.axis('off')
plt.show()


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Result&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

[[  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0. 255.   0.]
 [  0.   0.   0.   0.   0.]
 [  0.   0.   0.   0.   0.]]


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7fgcq703u5v24ymohm98.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7fgcq703u5v24ymohm98.png" alt="Image Result"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Sampling and Quantization
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Signals are sampled and quantized&lt;/li&gt;
&lt;li&gt;Sampling: spatial discretization&lt;/li&gt;
&lt;li&gt;Quantization: Discretization of values

&lt;ul&gt;
&lt;li&gt;Normally, 8bit 256 steps&lt;/li&gt;
&lt;li&gt;For special applications, such as medical use, 10-bit and 12-bit are also available.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sampling
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

image_downscaled1 = downscale_local_mean(im_gray, (1, 1))
image_downscaled2 = downscale_local_mean(im_gray, (10, 10))
image_downscaled3 = downscale_local_mean(im_gray, (20, 20))
image_downscaled4 = downscale_local_mean(im_gray, (50, 50))


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnnqdfmniffib5vso0scf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnnqdfmniffib5vso0scf.png" alt="Image Sampling "&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Quantization
&lt;/h3&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

# 32bit quantization
bins = np.linspace(0, im.max(), 2**5)
digi_image1 = np.digitize(im, bins)
digi_image1 = (np.vectorize(bins.tolist().__getitem__)(digi_image1-1).astype(int))

# 16bit quantization
bins = np.linspace(0, im.max(), 2**4)
digi_image2 = np.digitize(im, bins)
digi_image2 = (np.vectorize(bins.tolist().__getitem__)(digi_image2-1).astype(int))

# 8bit quantization
bins = np.linspace(0, im.max(), 2**3)
digi_image3 = np.digitize(im, bins)
digi_image3 = (np.vectorize(bins.tolist().__getitem__)(digi_image3-1).astype(int))

# 4bit quantization
bins = np.linspace(0, im.max(), 2**2)
digi_image4 = np.digitize(im, bins)
digi_image4 = (np.vectorize(bins.tolist().__getitem__)(digi_image4-1).astype(int))



&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;※If the quantization is made too coarse, non-existent contours &lt;strong&gt;"pseudo contours"&lt;/strong&gt; will be generated.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ybv0o4ghrjpd9svxa56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5ybv0o4ghrjpd9svxa56.png" alt="Image quantization "&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>python</category>
      <category>imageprocessing</category>
      <category>formyownuse</category>
    </item>
  </channel>
</rss>
