Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue10 #33

Merged
merged 2 commits into from
Mar 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
278 changes: 278 additions & 0 deletions Jupyter_Notebooks/Issue10_gaulghost.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,278 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Requirement already satisfied: imutils in e:\\anaconda\\lib\\site-packages (0.5.4)\n",
"Requirement already satisfied: opencv-python in e:\\anaconda\\lib\\site-packages (4.5.1.48)\n",
"Requirement already satisfied: numpy>=1.14.5 in e:\\anaconda\\lib\\site-packages (from opencv-python) (1.18.1)\n"
]
}
],
"source": [
"#Downloading important libraries\n",
"!pip install imutils\n",
"!pip install opencv-python"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#Importing Important Libraries\n",
"from skimage.measure import compare_ssim\n",
"import argparse\n",
"import imutils\n",
"import cv2\n",
"# from google.colab.patches import cv2_imshow"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Taking two Input Images to be compared\n",
"imageA = cv2.imread('img1.jpg')\n",
"imageB = cv2.imread('img2.jpg')\n",
"# Convert the Images to Grayscale\n",
"grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)\n",
"grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" ...,\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255]],\n",
"\n",
" [[ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" ...,\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255]],\n",
"\n",
" [[ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [245, 138, 94],\n",
" ...,\n",
" [243, 239, 238],\n",
" [243, 239, 238],\n",
" [ 0, 0, 255]],\n",
"\n",
" ...,\n",
"\n",
" [[ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 28, 28, 28],\n",
" ...,\n",
" [ 27, 27, 27],\n",
" [ 28, 28, 28],\n",
" [ 0, 0, 255]],\n",
"\n",
" [[ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 28, 28, 28],\n",
" ...,\n",
" [ 27, 27, 27],\n",
" [ 28, 28, 28],\n",
" [ 0, 0, 255]],\n",
"\n",
" [[ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" ...,\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255],\n",
" [ 0, 0, 255]]], dtype=uint8)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cnts = cv2.findContours(grayA, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)\n",
"cnts = imutils.grab_contours(cnts)\n",
"\n",
"i, j = 0, 0\n",
"[a, b, c, d] = [0, 0, 0, 0]\n",
"[e, f, g, k] = [0, 0, 0, 0]\n",
"\n",
"max, secondmax, s = 0,0,0\n",
"for c in cnts:\n",
" (x, y, w, h) = cv2.boundingRect(c)\n",
" if w*h > i*j:\n",
" [e, f, g, k] = [x, y, w, h]\n",
" i, j = w, h\n",
" else:\n",
" continue\n",
"\n",
"cv2.rectangle(imageA, (e, f), (e+g, f+k), (0, 0, 255), 2)\n",
"# cv2.imshow('',imageA)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"E:\\Anaconda\\lib\\site-packages\\ipykernel_launcher.py:3: UserWarning: DEPRECATED: skimage.measure.compare_ssim has been moved to skimage.metrics.structural_similarity. It will be removed from skimage.measure in version 0.18.\n",
" This is separate from the ipykernel package so we can avoid doing imports until\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"SSIM: 0.9933950857257461\n"
]
}
],
"source": [
"# compute the Structural Similarity Index (SSIM) between the two\n",
"# images, ensuring that the difference image is returned\n",
"(score, diff) = compare_ssim(grayA, grayB, full=True)\n",
"diff = (diff * 255).astype(\"uint8\")\n",
"print(\"SSIM: {}\".format(score))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"# threshold the difference image, followed by finding contours to\n",
"# obtain the regions of the two input images that differ\n",
"thresh = cv2.threshold(diff, 0, 255,\n",
"\tcv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]\n",
"cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,\n",
"\tcv2.CHAIN_APPROX_SIMPLE)\n",
"cnts = imutils.grab_contours(cnts)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"ans =0\n",
"for c in cnts:\n",
" (x, y, w, h) = cv2.boundingRect(c)\n",
" if w>9 and h>15:\n",
" ans+=1\n",
" cv2.rectangle(imageA, (x, y), (x+w, y+h), (0, 0, 255), 2)\n",
" cv2.rectangle(imageB, (x, y), (x+w, y+h), (0, 0, 255), 2)\n",
" else:\n",
" continue\n",
"# Uncomment below to see the images\n",
"# cv2.imshow('',imageA)\n",
"# cv2.waitKey(0)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"# cv2_imshow(imageB)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# cv2_imshow(diff)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
"# cv2_imshow(thresh)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"# If the value is greater than 7 the slide has changed\n",
"if(ans>7):\n",
" print('True')\n",
"else:\n",
" print('False')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Binary file added Jupyter_Notebooks/img1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Jupyter_Notebooks/img2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.