Clouds Removal
Cloud Removal using Opencv
Removal of white foreground using the hsv of white color and fast denoising techniques of opencv
Removal of white foreground using the hsv of white color and fast denoising techniques of opencv
Random Submission for Clouds Removal
Note : Create a copy of the notebook and use the copy for submission. Go to File > Save a Copy in Drive to create a new copy
Setting up Environment¶
Downloading Dataset¶
So we will first need to download the python library by AIcrowd that will allow us to download the dataset by just inputting the API key.
!pip install aicrowd-cli
%load_ext aicrowd.magic
%aicrowd login
# Downloading the Dataset
!rm -rf data
!mkdir data
!aicrowd dataset download -c clouds-removal "*Partial*" -o data
# Unzipping the dataset
!unzip data/train.zip -d data/train >> /dev/null
!unzip data/test.zip -d data/test >> /dev/null
Importing Libraries¶
# Importing Libraries
import os
from natsort import natsorted
from glob import glob
import cv2
from tqdm.notebook import tqdm
Generate Random Submission¶
In this section we will be generating a random submission. We will read all of the files from the tesing directroy and save the same video in clear
directory for submsision.
# Creating a clear directory
!rm -rf clear
!mkdir clear
import matplotlib.pyplot as plt
# Random submission function
import numpy as np
#a = img = np.zeros((512,512,3), dtype=np.uint8)
def random_submission(data_directory):
# List of all videos
video_files = natsorted(glob(data_directory))
# Groung through each video
for idx, img_file in enumerate(tqdm(video_files)):
# Saving a new video file
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(os.path.join("clear", f"clear_{idx}.mp4"), fourcc, 24.0, (512,512))
#print(img_file)
# Reading the video
img_video = cv2.VideoCapture(img_file)
#print(img_video)
# Going through each frame
ret=True
while ret:
# Reading the frame
ret, frame = img_video.read(cv2.IMREAD_UNCHANGED)
if frame is not None:
print('here')
# Convert RGB to grayscale:
grayscaleImage = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Convert the BGR image to HSV:
hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# HSV colors for white
lowerValues = np.array([180, 18, 255])
upperValues = np.array([0, 0, 231])
# Get binary mask of the blue ink:
bluepenMask = cv2.inRange(hsvImage, lowerValues, upperValues)
# Use a little bit of morphology to clean the mask:
# Set kernel (structuring element) size:
kernelSize = 3
# Set morph operation iterations:
opIterations = 1
# Get the structuring element:
morphKernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernelSize, kernelSize))
# Perform closing:
bluepenMask = cv2.morphologyEx(bluepenMask, cv2.MORPH_CLOSE, morphKernel, None, None, opIterations, cv2.BORDER_REFLECT101)
# Add the white mask to the grayscale image:
colorMask = cv2.add(grayscaleImage, bluepenMask)
_, binaryImage = cv2.threshold(colorMask, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
#cv2.imwrite('bwimage.jpg',binaryImage)
thresh, im_bw = cv2.threshold(binaryImage, 210, 230, cv2.THRESH_BINARY)
kernel = np.ones((1, 1), np.uint8)
imgfinal = cv2.dilate(im_bw, kernel=kernel, iterations=1)
# kernel = np.ones((10,10),np.uint8)
# res = cv2.erode(frame,kernel,iterations = 10)
out.write(imgfinal)
#plt.imshow(imgfinal)
#print(imgfinal.shape)
# # Adding the frame to video writer
#
else:
out.write(frame)
return frame
# Running the function
frame = random_submission("data/test/cloud*")
Submitting Results 📄¶
Uploading the Results¶
!aicrowd notebook submit -c clouds-removal -a clear --no-verify
Don't be shy to ask question related to any errors you are getting or doubts in any part of this notebook in discussion forum or in AIcrowd Discord sever, AIcrew will be happy to help you :)
Also, wanna give us your valuable feedback for next blitz or wanna work with us creating blitz challanges ? Let us know!
The HSV techniques and the opencv inbuilt desioning techniques were not that successful for cloud removal. The next approach that I am thinking of using is the CNN+GAN I will explain the concept in a new notebook
Content
Comments
You must login before you can post a comment.