<jupyter_start><jupyter_text><meta content="text/html; charset=ISO-8859-1"http-equiv="content-type"><spanstyle="font-weight: bold; text-decoration: underline;">Hackathon 2018 - chart-digitizer&nbsp;<spanstyle="font-style: italic;"><NAME><spanstyle="font-style: italic;"><NAME><spanstyle="font-style: italic;"><NAME><spanstyle="font-style: italic;"><NAME> 				 <meta content="text/html; charset=ISO-8859-1"http-equiv="content-type"><imgstyle="width: 50px; height: 15px;" alt="Python"src="https://upload.wikimedia.org/wikipedia/commons/thumb/f/f8/Python_logo_and_wordmark.svg/260px-Python_logo_and_wordmark.svg.png"><img alt="OpenCV"src="https://a.fsdn.com/allura/p/opencvlibrary/icon" /><img style="width: 50px; height: 15px;"alt="Anaconda"src="https://upload.wikimedia.org/wikipedia/en/thumb/c/cd/Anaconda_Logo.png/200px-Anaconda_Logo.png">    - Random chart generator   - Model training using object detection   - Result analysis<jupyter_code>import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import matplotlib<jupyter_output>/home/nbuser/anaconda3_420/lib/python3.5/site-packages/matplotlib/font_manager.py:281: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
  'Matplotlib is building the font cache using fc-list. '<jupyter_text>Random bar chart generator<jupyter_code>data = pd.DataFrame(data=np.random.rand(5,1), index=range(1,6), columns=['Fred'])
m,n = np.shape(data)

plt.clf()
plt.bar(x=data.index.values, height=data.values.ravel(), color='k') # figsize=(10, 6))
# Options for later from https://matplotlib.org/api/_as_gen/matplotlib.pyplot.bar.html
# bar_width = 0.35
# alpha = .3
fig=plt.gcf()
fig.set_size_inches(3, 2)
plt.axis('off')
fig.tight_layout()
fig.canvas.draw()
# grab the pixel buffer and dump it into a numpy array
pixels = np.array(fig.canvas.renderer._renderer)

plt.plot();<jupyter_output><empty_output><jupyter_text>Display generated chart<jupyter_code>print(pixels);
print(data);
y, X = img_gen_bar()
print(y)

#for neural net
X=X/255

#for DNN only
#X=X.reshape(1,-1,3)

#data={}
#for i in range(1000) :
#    data[i] = (generate_bar_chart() )<jupyter_output>[[[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]

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

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

 ...

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

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

 [[255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]
  ...
  [255 255 255   0]
  [255 255 255   0]
  [255 255 255   0]]]<jupyter_text>For historical reasons, OpenCV defaults to BGR format instead of usual RGB Lets convert OpenCV image to RGB consistentlyThe Lab color space has three components. L – Lightness ( Intensity ).a – color component ranging from Green to Magenta.b – color component ranging from Blue to Yellow. The Lab color space is quite different from the RGB color space. In RGB color space the color information is separated into three channels but the same three channels also encode brightness information. On the other hand, in Lab color space, the L channel is independent of color information and encodes brightness only. The other two channels encode color.<jupyter_code>cvimrgb = cv2.cvtColor(cvim2disp,cv2.COLOR_BGR2RGB)

#or
#imbgr = cv2.cvtColor(im2disp,cv2.COLOR_RGB2BGR)

figure()
imshow(cvimrgb)

cvimlab = cv2.cvtColor(cvim2disp,cv2.COLOR_BGR2LAB)

#or
#imbgr = cv2.cvtColor(im2disp,cv2.COLOR_RGB2BGR)

figure()
imshow(cvimlab)<jupyter_output><empty_output><jupyter_text>Useful utility function<jupyter_code>img = cv2.imread('sample-1.png', 0)
img = cv2.threshold(img, 100, 255, cv2.THRESH_BINARY)[1]  # ensure binary
ret, labels = cv2.connectedComponents(img)

# Map component labels to hue val
label_hue = np.uint8(179*labels/np.max(labels))
blank_ch = 255*np.ones_like(label_hue)
labeled_img = cv2.merge([label_hue, blank_ch, blank_ch])

# cvt to BGR for display
labeled_img = cv2.cvtColor(labeled_img, cv2.COLOR_HSV2BGR)

# set bg label to black
labeled_img[label_hue==0] = 0

figure()
imshow( labeled_img)<jupyter_output><empty_output><jupyter_text>Simple filtering example<jupyter_code>im2disp = imread('sample-1.png')

blurred = cv2.GaussianBlur(im2disp,(19,19),0)

figure()
imshow(blurred)


#more general method
kernel = np.ones((5,5),np.float32)/25
blurred2 = cv2.filter2D(im2disp,-1,kernel)

figure()
imshow(blurred2)<jupyter_output><empty_output><jupyter_text>Converting to LAB<jupyter_code>cv2.imwrite('data/mycvimage.png', cvim2disp)
#or
imsave('data/myimage.png',im2disp)

x=2
%whos<jupyter_output>Variable    Type       Data/Info
--------------------------------
blurred     ndarray    303x328x4: 397536 elems, type `float32`, 1590144 bytes (1.5164794921875 Mb)
blurred2    ndarray    303x328x4: 397536 elems, type `float32`, 1590144 bytes (1.5164794921875 Mb)
cv2         module     <module 'cv2' from '/User<...>2.cpython-35m-darwin.so'>
cvim2disp   ndarray    303x328x3: 298152 elems, type `uint8`, 298152 bytes (291.1640625 kb)
cvimrgb     ndarray    303x328x3: 298152 elems, type `uint8`, 298152 bytes (291.1640625 kb)
im2disp     ndarray    303x328x4: 397536 elems, type `float32`, 1590144 bytes (1.5164794921875 Mb)
kernel      ndarray    5x5: 25 elems, type `float32`, 100 bytes
x           int        2<jupyter_text>1 numpy gotcha for people coming from Matlab<jupyter_code>x = zeros(5)
y = x

y[1] = 1

#uncomment next line and run
print(x)<jupyter_output>[0. 1. 0. 0. 0.]<jupyter_text>What happened?  Why did modifying y change x?A: Python copies arrays and other mutable data types by reference by defaultHere's what you probably want:<jupyter_code>x=zeros(5)
y=x.copy()

y[1] = 1

print(x)<jupyter_output>[0. 0. 0. 0. 0.]<jupyter_text>Let's run some of the included OpenCV examples<jupyter_code>%run inpaint.py

%run deconvolution.py

%run find_obj.py

%run peopledetect.py

cd python<jupyter_output>/Users/prabhatranjan/sources/opencv/CASIS-OpenCV-Course-master/python
StarcoderdataJupyterStructuredCleanDedup