<jupyter_start><jupyter_text>Reading the images into a numpy array<jupyter_code>import glob
negative_image = glob.glob('/content/Negative/*')
positive_image = glob.glob('/content/Positive/*')
negative_image[0]
from IPython.display import Image
from keras.preprocessing.image import load_img 
import warnings 
from keras.preprocessing.image import img_to_array 
import numpy as np
import pandas as pd
import numpy as np
import cv2
import matplotlib.pyplot as plt
from google.colab.patches import cv2_imshow
image = load_img(positive_image[0],color_mode = 'grayscale', target_size=(50,50)) 
print(type(image)) 
print(image.format) 
print(image.mode) 
print(image.size) 
display(image)
from keras.preprocessing.image import load_img 
import warnings 
from keras.preprocessing.image import img_to_array 
import numpy as np
import pandas as pd

class MissingData(Exception):
  def __init(self,message):
    self.message = message
    super().__init__(self.message)
    

def images_to_dataframe(image_path_list):
  if len(image_path_list)==0 or type(image_path_list) == "undefined":
    raise MissingData('\nException: \nMissing Image List:\nEmpty list \
    is passed or no list passed.')
  else:
    parsed_image = []
    for i in image_path_list:
      image = load_img(i,color_mode = 'grayscale', target_size=(50,50)) 

      image_array = img_to_array(image) / 255.

      onedarr = np.ravel(image_array)

      parsed_image.append(onedarr)
    if len(parsed_image)>0:
      df = pd.DataFrame(parsed_image)
    else:
      raise Exception('Exception: \nParsed Image List is Empty.')
  return df
from keras.preprocessing.image import load_img 
import warnings 
from keras.preprocessing.image import img_to_array 
import numpy as np
import pandas as pd

class MissingData(Exception):
  def __init(self,message):
    self.message = message
    super().__init__(self.message)
    

def images_to_dataframe1(image_path_list):
  if len(image_path_list)==0 or type(image_path_list) == "undefined":
    raise MissingData('\nException: \nMissing Image List:\nEmpty list \
    is passed or no list passed.')
  else:
    parsed_image = []
    for i in image_path_list:
      img0 = cv2.imread(i)

# converting to gray scale
      gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)

# remove noise
      #img = cv2.GaussianBlur(gray,(3,3),0)
      #laplacian = cv2.Laplacian(gray,cv2.CV_64F)
      #sobelx = cv2.Sobel(gray,cv2.CV_64F,1,0,ksize=5)
      sobely = cv2.Sobel(gray,cv2.CV_64F,0,1,ksize=5)
# image_gaus = cv2.GaussianBlur(image,(3,3),0)
      img1 = cv2.resize(sobely, (50,50), interpolation = cv2.INTER_AREA)

      img2 = img1/255.

      onedarr = np.ravel(img2)

      parsed_image.append(onedarr)
    if len(parsed_image)>0:
      df = pd.DataFrame(parsed_image)
    else:
      raise Exception('Exception: \nParsed Image List is Empty.')
  return df
negative_df2 = images_to_dataframe1(negative_image)
negative_df2
positive_df2 = images_to_dataframe1(positive_image)
positive_df2
negative_df2['target'] = [0]*20000
positive_df2['target'] = [1]*20000
data2 = pd.concat([negative_df2,positive_df2])
data2.to_csv('sobely.csv')
!mv '/content/sobely.csv' '/content/drive/My Drive'
data2.info()
#images_to_dataframe(negative_image[1:100]).to_csv('negative.csv',mode='a')
negative_df = images_to_dataframe(negative_image)
negative_df['target'] = [0]*20000
positive_df = images_to_dataframe(positive_image)
positive_df['target'] = [1]*20000
data = pd.concat([negative_df,positive_df])
data.to_csv('data.csv')
!mv '/content/data.csv' '/content/drive/My Drive'
data.info()

ksize = 5  #Use size that makes sense to the image and fetaure size. Large may not be good. 
#On the synthetic image it is clear how ksize affects imgae (try 5 and 50)
sigma = 0.1 #Large sigma on small features will fully miss the features. 
theta = 3*np.pi/4  #/4 shows horizontal 3/4 shows other horizontal. Try other contributions
lamda = 1*np.pi /4  #1/4 works best for angled. 
gamma=0.2  #Value of 1 defines spherical. Calue close to 0 has high aspect ratio
#Value of 1, spherical may not be ideal as it picks up features from other regions.
phi = 0  #Phase offset. I leave it to 0. 


kernel = cv2.getGaborKernel((ksize, ksize), sigma, theta, lamda, gamma, phi, ktype=cv2.CV_32F)

img = cv2.imread(positive_image[0])
#img = cv2.imread('BSE_Image.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
fimg = cv2.filter2D(img, cv2.CV_8UC3, kernel)

kernel_resized = cv2.resize(kernel, (400, 400))                    # Resize image
cv2_imshow(kernel_resized)
cv2_imshow(img)
cv2_imshow(fimg)
import cv2
import numpy as np
from matplotlib import pyplot as plt

# loading image
#img0 = cv2.imread('SanFrancisco.jpg',)
img0 = cv2.imread(positive_image[0])

# converting to gray scale
gray = cv2.cvtColor(img0, cv2.COLOR_BGR2GRAY)

# remove noise
img = cv2.GaussianBlur(gray,(3,3),0)

# convolute with proper kernels
laplacian = cv2.Laplacian(img,cv2.CV_64F)
sobelx = cv2.Sobel(img,cv2.CV_64F,1,0,ksize=5)  # x
sobely = cv2.Sobel(img,cv2.CV_64F,0,1,ksize=5)  # y
canny_edges = cv2.Canny(img,100,200)

# plt.subplot(3,2,1),plt.imshow(img,cmap = 'gray')
# plt.title('Original'), plt.xticks([]), plt.yticks([])
# plt.subplot(3,2,2),plt.imshow(laplacian,cmap = 'gray')
# plt.title('Laplacian'), plt.xticks([]), plt.yticks([])
# plt.subplot(3,2,3),plt.imshow(sobelx,cmap = 'gray')
# plt.title('Sobel X'), plt.xticks([]), plt.yticks([])
# plt.subplot(3,2,4),plt.imshow(sobely,cmap = 'gray')
# plt.title('Sobel Y'), plt.xticks([]), plt.yticks([])
# plt.subplot(3,2,5),plt.imshow(canny_edges,cmap = 'gray')
# plt.title('Canny Edge'), plt.xticks([]), plt.yticks([])

plt.imshow(canny_edges,cmap="gray")
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup