Skip to content

Instantly share code, notes, and snippets.

Created June 16, 2022 14:38
Show Gist options
  • Save jaskiratsingh2000/3a9734caeb1195dd0f55e506594aea83 to your computer and use it in GitHub Desktop.
Save jaskiratsingh2000/3a9734caeb1195dd0f55e506594aea83 to your computer and use it in GitHub Desktop.
import os
import argparse
import cv2
from glob import glob
import numpy as np
def loadData():
#path for the groundtruth images and masks
groundTruthImagePath = "/Users/jaskiratsingh/newDC/images/20.png"
predictedMaskImagePath = "/Users/jaskiratsingh/newDC/masks/20.png"
#Converting images into mask array
groundTruthImageArray = cv2.cvtColor((cv2.imread(groundTruthImagePath)),cv2.COLOR_BGR2GRAY)
predictedMaskImageArray = cv2.cvtColor((cv2.imread(predictedMaskImagePath)),cv2.COLOR_BGR2GRAY)
#Finding the height of the Images and Predicted Mask
heightOfGroundImage = groundTruthImageArray.shape[0]
widthOfGroundImage = groundTruthImageArray.shape[1]
sizeOfGroundImage = (heightOfGroundImage, widthOfGroundImage)
#Resizing the prdicted mask image in a shape equivalent to ground truth
predictedMaskImageArray = cv2.resize(predictedMaskImageArray,sizeOfGroundImage, interpolation = cv2.INTER_LINEAR)
predictedMaskImageArray = np.transpose(predictedMaskImageArray)
#Finding the Height and Width of the Predicted Mask
heightOfPredictedMask = predictedMaskImageArray.shape[0]
widthOfPredictedMask = predictedMaskImageArray.shape[1]
#Converting the list into arrays and storing it in a new Variable for respective sizes
groundTruthImageSize = np.asarray([heightOfGroundImage, widthOfGroundImage])
predictedMaskImageSize = np.asarray([heightOfPredictedMask, widthOfPredictedMask])
print("Ground Truth Image size is: ",groundTruthImageSize)
print("Predicted Mask Image size is: ",predictedMaskImageSize)
#return imagesGroundArray, maskPredArray
return groundTruthImageSize, predictedMaskImageSize, groundTruthImageArray, predictedMaskImageArray
def diceCoefficient(groundTruthImage, predictedMaskImage, groundTruthImageArray, predictedMaskImageArray):
heightOfGroundImage = groundTruthImage[0]
widthOfGroundImage = groundTruthImage[1]
positive = 0
negative = 0
i = 0
j = 0
for i in range(heightOfGroundImage):
for j in range(widthOfGroundImage):
if groundTruthImageArray[i][j] == predictedMaskImageArray[i][j]:
positive = positive + 1
negative = negative + 1
j = j + 1
i = i + 1
#applying the formula
return (2 * positive) / (positive + negative)
if __name__ == "__main__":
groundTruthImage, predictedMaskImage, groundTruthImageArray, predictedMaskImageArray = loadData()
diceCoefficient = diceCoefficient(groundTruthImage, predictedMaskImage, groundTruthImageArray, predictedMaskImageArray)
print("Dice Coefficient is:", diceCoefficient)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment