├── Images ├── 1AF-1604.JPG ├── 1AF-1804.JPG ├── 1AF-2204.JPG ├── 1AF-3004.JPG ├── 1AR-1604.JPG ├── 1AR-1804.JPG ├── 1AR-2004.JPG ├── 1AR-2804.JPG ├── 1AR-3004.JPG └── ezgif.com-gif-maker.gif ├── LICENSE ├── README.md ├── Rust_Detection.py └── Tutorial.md /Images/1AF-1604.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AF-1604.JPG -------------------------------------------------------------------------------- /Images/1AF-1804.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AF-1804.JPG -------------------------------------------------------------------------------- /Images/1AF-2204.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AF-2204.JPG -------------------------------------------------------------------------------- /Images/1AF-3004.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AF-3004.JPG -------------------------------------------------------------------------------- /Images/1AR-1604.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AR-1604.JPG -------------------------------------------------------------------------------- /Images/1AR-1804.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AR-1804.JPG -------------------------------------------------------------------------------- /Images/1AR-2004.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AR-2004.JPG -------------------------------------------------------------------------------- /Images/1AR-2804.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AR-2804.JPG -------------------------------------------------------------------------------- /Images/1AR-3004.JPG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/1AR-3004.JPG -------------------------------------------------------------------------------- /Images/ezgif.com-gif-maker.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kaustubh-Beta/Rust-detection-algorithmm/2f9bf420e1ab1a914a5d7a9e9d2a281e138dad7d/Images/ezgif.com-gif-maker.gif -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 kaustubh-Beta 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Rust Detection Algorithm 2 | 3 | ![Recordit GIF](Images/ezgif.com-gif-maker.gif 4 | ) 5 | 6 | The rust detection algorithm uses colour based image processing technique to detect and segment 7 | the region of metal affected by rust. I developed this code for my friend who was studying 8 | the effect of a solution on different metals to preventing rusting. 9 | 10 | Using the algorithm, it was easy to compare the number of pixels corresponding to 11 | rusted area of metal. Since the arrangement of camera while taking the image is fixed, comparison 12 | directly in terms of pixels will work well. 13 | 14 | >- You can learn some key functions of opencv from this code. 15 | >- Refer a detailed [Tutorial](Tutorial.md) explaining the [code](Rust_Detection.py). 16 | 17 | --- 18 | 19 | 20 | 21 | ## Installation 22 | 23 | #### Clone 24 | 25 | - Clone this repo to your local machine using `https://github.com/kaustubh-Beta/Rust-detection-algorithmm.git` 26 | 27 | #### Setup/Requirements 28 | 29 | > The code is written in Python (`Python3`) 30 | > You need the following libraries 31 | > - numpy 32 | > - OpenCV 33 | --- 34 | 35 | ##### Refer the [Tutorial](Tutorial.md) for detailed explanation of the code. 36 | 37 | 38 | 39 | ## License 40 | 41 | [![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org) 42 | 43 | - **[MIT license](http://opensource.org/licenses/mit-license.php)** 44 | -------------------------------------------------------------------------------- /Rust_Detection.py: -------------------------------------------------------------------------------- 1 | # this is a project based on detection of rust iron portion 2 | # you need to pass an argument stating the file name of the image. 3 | # for simplicity keep the script and image in the same folder 4 | 5 | import cv2 6 | from sys import argv 7 | import numpy as np 8 | import os 9 | import glob 10 | 11 | count = 0 12 | 13 | def rust_detect(file): 14 | A = cv2.imread(file) 15 | img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 16 | 17 | # Range for lower red 18 | lower_red = np.array([0,70,70]) 19 | upper_red = np.array([20,200,150]) 20 | mask0 = cv2.inRange(img_hsv, lower_red, upper_red) 21 | 22 | # range for upper red 23 | lower_red = np.array([170,70,70]) 24 | upper_red = np.array([180,200,150]) 25 | mask1 = cv2.inRange(img_hsv, lower_red, upper_red) 26 | 27 | # add both masks 28 | mask = mask0+mask1 29 | 30 | output_img = cv2.bitwise_and(img,img,mask=mask) 31 | 32 | print("\n\n\n Number of pixels depicting rust \n >> %d"%(np.sum(mask)/255)) 33 | cv2.imshow('image1',output_img) 34 | cv2.imshow('image2',img) 35 | cv2.waitKey(0) 36 | cv2.imwrite('output_image%d.jpg'%count,output_img) 37 | cv2.imwrite('image%d.jpg'%count,img) 38 | cv2.destroyAllWindows() 39 | os.system("cls") 40 | 41 | 42 | 43 | os.system("color 0a") 44 | os.system("cls") 45 | 46 | print(""" Welcome to the rust detection software!! 47 | The software detects the rusted portion of metal 48 | and calculates nuber of rust piels for 49 | comparitive analysis.\n\n""") 50 | print("**********************************************") 51 | 52 | images = glob.glob("Images/*.jpg") 53 | 54 | for path in images: 55 | count+=1 56 | rust_detect(path) 57 | 58 | input("\n PRESS ENTER TO EXIT ") 59 | -------------------------------------------------------------------------------- /Tutorial.md: -------------------------------------------------------------------------------- 1 | # Rust Detection Algorithm Tutorial 2 | 3 | This tutorial aims at explaining some key concepts of `image processing` using `opencv` with python. 4 | We will try to understand each line of the [code](Air_Drums.py) 5 | 6 | > ###### Note for someone using python for the first time: 7 | > anything written after '#' will be treated as a comment. 8 | > it is a good practice to add comments in your code. 9 | > It makes the code easy to understand. 10 | 11 | 12 | ### Importing all the required libraries 13 | 14 | ```python 15 | import cv2 16 | from sys import argv 17 | import numpy as np 18 | import os 19 | import glob 20 | ``` 21 | 22 | ### Understanding the rust_detect() function 23 | 24 | First we import the image and save it in form of an array. 25 | Since the setup for analysis and image capturing is fixed 26 | we do not need to worry about image resolution issue. 27 | 28 | ```python 29 | # Importing the image 30 | A = cv2.imread(file) 31 | ``` 32 | 33 | #### Converting image from BGR to HSV 34 | 35 | We convert the image into HSV colour space using the following line of code. 36 | ```python 37 | img_hsv=cv2.cvtColor(img, cv2.COLOR_BGR2HSV) 38 | ``` 39 | `Why transform the image into HSV colour space ??` 40 | Because the algorithm detects the rust based on its property of colour. 41 | Rusted area for this specific metal had a specific colour range 42 | (`grey-brown` represent lower level of rust while `Dark reddish-brown` 43 | represent sever rusting). Using HSV(Hue-Saturation-Value) colour space we can distinguish 44 | between different colours much accurately than we can in RGB colour space. 45 | 46 | #### Setting range of HSV values for rust detection 47 | Below code is used to detect pixels corresponding to rusted metal surface. 48 | ```python 49 | # Range for lower red 50 | lower_red = np.array([0,70,70]) 51 | upper_red = np.array([20,255,150]) 52 | mask0 = cv2.inRange(img_hsv, lower_red, upper_red) 53 | 54 | # range for upper red 55 | lower_red = np.array([220,70,70]) 56 | upper_red = np.array([255,255,150]) 57 | mask1 = cv2.inRange(img_hsv, lower_red, upper_red) 58 | mask = mask0+mask1 59 | ``` 60 | 61 | Hue range | 0-20 | 170-180 62 | ---|---|--- 63 | 64 | >The Hue values actually range between 0-360 degrees but 65 | >in OpenCV to fit into 8bit value the range is from 0-180. 66 | >Red colour is represented by 0-20 and 170-180 values. 67 | 68 | 69 | Saturation range | 70-200 70 | ---|--- 71 | > Saturation represents purity of colour. Pure Red, Green and Blue 72 | >are considered to be true saturated colours. As saturation decreases the effect of the other two 73 | >colour component increases. 74 | 75 | Value range | 70-150 76 | ---|--- 77 | > Value corresponds to the brightness of the image. For a given pixel if the value is increased or 78 | > decreased then values of R,G and B will increase or decrease respectively but their percentage 79 | >contribution will remain unchanged. 80 | 81 | Now we understand why we need to convert to HSV colour space. 82 | 83 | The below command returns an array with pixel value = 255 for pixels 84 | having HSV values within upper and lower value range and 0 otherwise. 85 | This way we generate a mask. 86 | ```python 87 | mask = cv2.inRange(img_hsv, lower_red, upper_red) 88 | ``` 89 | 90 | And finally we `OR` both the masks (mask0 and mask1) to get 91 | our final mask. 92 | ```python 93 | mask = mask0 + mask 94 | ``` 95 | This is also a simple example of operator overloading of `+`. 96 | 97 | 98 | 99 | 100 | #### Calculating the number of rust pixels 101 | To calculate the total pixesl corresponding to rusted metal region 102 | we simply find the number of white pixels in the final mask. 103 | --------------------------------------------------------------------------------