Skip to content

Instantly share code, notes, and snippets.

@liamwhite
Created March 8, 2017 23:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save liamwhite/b023cdba4738e911293a8c610b98f987 to your computer and use it in GitHub Desktop.
Save liamwhite/b023cdba4738e911293a8c610b98f987 to your computer and use it in GitHub Desktop.
# Perceptual image deduplication based on average intensity.
module ImageAnalyzer
def intensities(image_path)
image = CvMat.load(image_path, CV_LOAD_IMAGE_COLOR)
image = image.smooth(CV_GAUSSIAN, 3, 3)
half_x = (image.cols / 2).round
half_y = (image.rows / 2).round
nw_rect = image.sub_rect(0, 0, half_x, half_y)
ne_rect = image.sub_rect((half_x + 1), 0, (image.cols - (half_x + 1)), half_y)
sw_rect = image.sub_rect(0, (half_y + 1), half_x, (image.rows - (half_y + 1)))
se_rect = image.sub_rect((half_x + 1), (half_y + 1), (image.cols - (half_x + 1)), (image.rows - (half_y + 1)))
{
average_intensity: rect_sum(image),
nw_intensity: rect_sum(nw_rect),
ne_intensity: rect_sum(ne_rect),
sw_intensity: rect_sum(sw_rect),
se_intensity: rect_sum(se_rect)
}
end
def rect_sum(rect)
sums = rect.sum
r = (sums[0] / (rect.rows * rect.cols)) * 0.2126
g = (sums[1] / (rect.rows * rect.cols)) * 0.7152
b = (sums[2] / (rect.rows * rect.cols)) * 0.0772
((r + g + b) / 3).round(6)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment