receipt_indexer/code/autocropper/notebooks/oldnotebooks/c++cropper.ipynb
Ethan Wellenreiter 423b511dd9 Cleanup commit
Moving around the testing notebooks. Autocropping is about done
with exception to any new versions or converting the stuff to C
code.

Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
2023-10-18 22:48:24 -04:00

431 lines
13 KiB
Plaintext

{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"## ORIGINAL FILE FOR SELECTIVE SEGMENTATION SEARCH"
]
},
{
"cell_type": "code",
"execution_count": 350,
"metadata": {},
"outputs": [],
"source": [
"import cv2\n",
"import numpy as np\n",
"from queue import PriorityQueue\n",
"import myfunctions as mf\n",
"\n",
"import matplotlib.pyplot as plt\n",
"import random"
]
},
{
"cell_type": "code",
"execution_count": 351,
"metadata": {},
"outputs": [],
"source": [
"# def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA):\n",
"# dim = None\n",
"# (h, w) = image.shape[:2]\n",
"\n",
"# if width is None and height is None:\n",
"# return image\n",
"# if width is None:\n",
"# r = height / float(h)\n",
"# dim = (int(w * r), height)\n",
"# else:\n",
"# r = width / float(w)\n",
"# dim = (width, int(h * r))\n",
"\n",
"# return cv2.resize(image, dim, interpolation=inter)"
]
},
{
"cell_type": "code",
"execution_count": 352,
"metadata": {},
"outputs": [],
"source": [
"import heapq as hq\n",
"\n",
"class MaxHeapObj(object):\n",
" def __init__(self, val): self.val = val\n",
" def __lt__(self, other): return self.val > other.val\n",
" def __eq__(self, other): return self.val == other.val\n",
" def __str__(self): return str(self.val)\n",
" \n",
"class MinHeap(object):\n",
" def __init__(self): self.h = []\n",
" def heappush(self, x): heapq.heappush(self.h, x)\n",
" def heappop(self): return heapq.heappop(self.h)\n",
" def __getitem__(self, i): return self.h[i]\n",
" def __len__(self): return len(self.h)\n",
" \n",
"class MaxHeap(MinHeap):\n",
" def heappush(self, x): heapq.heappush(self.h, MaxHeapObj(x))\n",
" def heappop(self): return heapq.heappop(self.h).val\n",
" def __getitem__(self, i): return self.h[i].val"
]
},
{
"cell_type": "code",
"execution_count": 353,
"metadata": {},
"outputs": [],
"source": [
"# def clip(n, lower, upper):\n",
"# return max(lower, min(n, upper))\n",
"\n",
"# def colourscaler(n, min, max):\n",
"# temp = n-min\n",
"# diff = abs(max - min)\n",
"# return clip((temp/diff)*255, 0, 255)"
]
},
{
"cell_type": "code",
"execution_count": 354,
"metadata": {},
"outputs": [],
"source": [
"# inline double clip(double n, double lower, double upper) {\n",
"# return std::max(lower, std::min(n, upper));\n",
"# };\n",
"\n",
"# inline double colourscaler(double n, double min, double max) {\n",
"# double temp = n - min;\n",
"# double diff = std::abs(max - min);\n",
"# return clip((temp / diff) * 255, 0, 255);\n",
"# };"
]
},
{
"cell_type": "code",
"execution_count": 355,
"metadata": {},
"outputs": [],
"source": [
"# ## Test this code for the masking/colour squishing. it essentially can just speed up clipping the edges.\n",
"# #!/usr/local/bin/python3\n",
"# import cv2 as cv\n",
"# import numpy as np\n",
"\n",
"# # Load the aerial image and convert to HSV colourspace\n",
"# image = cv.imread(\"aerial.png\")\n",
"# hsv=cv.cvtColor(image,cv.COLOR_BGR2HSV)\n",
"\n",
"# # Define lower and uppper limits of what we call \"brown\"\n",
"# brown_lo=np.array([10,0,0])\n",
"# brown_hi=np.array([20,255,255])\n",
"\n",
"# # Mask image to only select browns\n",
"# mask=cv.inRange(hsv,brown_lo,brown_hi)\n",
"\n",
"# # Change image to red where we found brown\n",
"# image[mask>0]=(0,0,255)\n",
"\n",
"# cv.imwrite(\"result.png\",image)\n",
"\n",
"#CAN ALSO TRY USING NUMPY VECTORIZATION"
]
},
{
"cell_type": "code",
"execution_count": 356,
"metadata": {},
"outputs": [],
"source": [
"# def rotate(img, angle):\n",
"# rows,cols = img.shape[0], img.shape[1]\n",
"# M = cv2.getRotationMatrix2D((cols/2,rows/2),angle,1)\n",
"# dst = cv2.warpAffine(img,M,(cols,rows))\n",
"# return dst"
]
},
{
"cell_type": "code",
"execution_count": 357,
"metadata": {},
"outputs": [],
"source": [
"def crop(image, lower = 100, upper = 255, threshold1 = 50, threshold2 = 350):\n",
" lower = max(0,lower)\n",
" upper = min(255, upper)\n",
" gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)\n",
"\n",
" scaled_gray = np.zeros(gray.shape, gray.dtype)\n",
" \n",
" # for y in range(0,gray.shape[0]):\n",
" # for x in range(0,gray.shape[1]):\n",
" # scaled_gray[y][x] = colourscaler(gray[y][x], lower, upper)\n",
" scaled_gray = gray\n",
" \n",
" blurred = cv2.GaussianBlur(scaled_gray, (15,15),0)\n",
" # blurred = scaled_gray\n",
" edged = cv2.Canny(blurred, threshold1, threshold2)\n",
" # meangrayscale = cv2.mean(scaled_gray)[0]\n",
" # print(meangrayscale)\n",
" # edged = cv2.Canny(blurred, int(meangrayscale*2), int(meangrayscale*4))\n",
" return edged\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 358,
"metadata": {},
"outputs": [],
"source": [
"def selectiveSearchSegmentationImp(image):\n",
" ss = cv2.ximgproc.segmentation.createSelectiveSearchSegmentation()\n",
" ss.setBaseImage(image)\n",
" ss.switchToSelectiveSearchFast()\n",
" return ss.process()"
]
},
{
"cell_type": "code",
"execution_count": 359,
"metadata": {},
"outputs": [],
"source": [
"img = cv2.imread('./testing_space/final.jpg')"
]
},
{
"cell_type": "code",
"execution_count": 360,
"metadata": {},
"outputs": [],
"source": [
"# def rectArea(rect):\n",
"# # print(rect)\n",
"# return rect[2]*rect[3]\n",
"\n",
"# def biggestRects(n, rects):\n",
"# dict = {}\n",
"# # outrects = np.zeros(shape=(n, 4))\n",
"# for rect in rects:\n",
"# dict[tuple(rect)] = mf.rectArea(rect)\n",
"# # maxh.heappush(mf.rectArea(rect))\n",
"# # print(maxh[0])\n",
" \n",
" \n",
"# heap = [(-value, key) for key,value in dict.items()]\n",
"# largest = hq.nsmallest(n, heap)\n",
" \n",
"\n",
"# # hq.heapify(list(dict.items()))\n",
"# # for i in range(0,n):\n",
"# # outrects[i] = maxh.heappop()\n",
"# # print(outrects)\n",
"# return [key for value, key in largest]\n",
"\n",
"# def overlapRect(rects):\n",
"# leftwall = -1\n",
"# rightwall = -1\n",
"# topwall = -1\n",
"# bottomwall = -1\n",
"# for (x, y, w, h) in rects:\n",
"# if (leftwall == -1):\n",
"# leftwall = x\n",
"# rightwall = x + w\n",
"# topwall = y\n",
"# bottomwall = y + h\n",
"# continue\n",
"# leftwall = max(leftwall, x)\n",
"# rightwall = min(rightwall, x+w)\n",
"# topwall = max(topwall, y)\n",
"# bottomwall = min(bottomwall, y+h)\n",
" \n",
"# if (topwall >= bottomwall or leftwall >= rightwall):\n",
"# return (-1, -1, -1, -1)\n",
"# return (leftwall, topwall, rightwall-leftwall, bottomwall-topwall)"
]
},
{
"cell_type": "code",
"execution_count": 344,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(-1, -1, -1, -1)\n"
]
}
],
"source": [
"# rect = crop(img)\n",
"\n",
"# _, thresholded = cv2.threshold(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), 200, 255, cv2.THRESH_BINARY)\n",
"\n",
"rects = selectiveSearchSegmentationImp(cv2.GaussianBlur(ResizeWithAspectRatio(img,300), (15,15),0))\n",
"# mf.rectArea(rects[0])\n",
"bigRects = mf.biggestRects(20, rects)\n",
"# print(bigRects)\n",
"\n",
"finalrect = mf.overlapRect(bigRects)\n",
"print(finalrect)\n",
"output = ResizeWithAspectRatio(img,300)\n",
"for (x, y, w, h) in [finalrect]:\n",
"\t\t# draw the region proposal bounding box on the image\n",
"\t\tcolor = [random.randint(0, 255) for j in range(0, 3)]\n",
"\t\tcv2.rectangle(output, (x, y), (x + w, y + h), color, 2)\n",
"\n",
"# edges = cv2.Canny(cv2.GaussianBlur(cv2.cvtColor(img, cv2.COLOR_BGR2GRAY), (15,15),0),255 / 4, 255)\n",
"\n",
"# plt.imshow(edges, cmap='gray', vmin=0, vmax=255)\n",
"# plt.show()\n",
"\n",
"cv2.imshow(\"banana\", output)\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()\n",
"\n",
"\n",
"# print(range(0,img.shape[1]))\n",
"# for i in range(0,img.shape[1]):\n",
"# print(i)"
]
},
{
"cell_type": "code",
"execution_count": 389,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"temp = ResizeWithAspectRatio(crop(img, threshold1=150, threshold2=350),500)\n",
"contours, _ = cv2.findContours(temp, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
"# print(type(contours))\n",
"# max(cv2.contourArea(contours))\n",
"# areas = list(map(cv2.contourArea, contours))\n",
"# print(areas)\n",
"contourindex = np.argmax(list(map(cv2.contourArea, contours)))\n",
"temp = cv2.drawContours(temp, contours, contourindex, (255,0,0), 2)\n",
"cv2.imshow(\"banana\", temp)\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()\n",
"print(contourindex)\n",
"rect = cv2.boundingRect(contours[contourindex])\n",
"color = (random.randint(0,256), random.randint(0,256), random.randint(0,256))\n",
"result = cv2.rectangle(ResizeWithAspectRatio(img,500), rect, color, 3)"
]
},
{
"cell_type": "code",
"execution_count": 362,
"metadata": {},
"outputs": [],
"source": [
"# print(contourindex)"
]
},
{
"cell_type": "code",
"execution_count": 371,
"metadata": {},
"outputs": [],
"source": [
"cv2.imshow(\"banana\", result)\n",
"cv2.waitKey(0)\n",
"cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 348,
"metadata": {},
"outputs": [],
"source": [
"# HSV = cv2.cvtColor(ResizeWithAspectRatio(img,500), cv2.COLOR_BGR2HSV)\n",
"# low = np.array([0,0,10])\n",
"# high = np.array([179,10,255])\n",
"\n",
"# mask = cv2.inRange(HSV,low,high)\n",
"\n",
"# cv2.imshow(\"banana\", mask)\n",
"# cv2.waitKey(0)\n",
"# cv2.destroyAllWindows()"
]
},
{
"cell_type": "code",
"execution_count": 349,
"metadata": {},
"outputs": [],
"source": [
" # cv::Mat gray, scaled_gray, blurred, edged;\n",
"\n",
" # lower = std::max(lower, 0);\n",
" # upper = std::min(upper, 255);\n",
"\n",
" # cv::cvtColor(src, gray, cv::COLOR_BGR2GRAY);\n",
" # scaled_gray = cv::Mat::zeros(gray.size(), gray.type());\n",
"\n",
" # for (int y = 0; y < gray.rows; y++) {\n",
" # for (int x = 0; x < gray.cols; x++) {\n",
" # scaled_gray.at<uchar>(y, x) =\n",
" # cv::saturate_cast<uchar>(colourscaler(gray.at<uchar>(y, x), lower, upper));\n",
" # }\n",
" # }\n",
"\n",
" # cv::GaussianBlur(scaled_gray, blurred, cv::Size(15, 15), 0);\n",
" # cv::Canny(blurred, edged, threshold1, threshold2);\n",
"\n",
" # std::vector<std::vector<cv::Point>> contours;\n",
" # std::vector<cv::Vec4i> heirarchy;\n",
" # cv::Mat approx;\n",
"\n",
" # cv::findContours(edged, contours, heirarchy, cv::RETR_TREE, cv::CHAIN_APPROX_SIMPLE);\n",
"\n",
" # cv::cvtColor(gray, gray, cv::COLOR_GRAY2BGR);\n",
"\n",
" # std::sort(contours.begin(), contours.end(), [](std::vector<cv::Point> a, std::vector<cv::Point> b) {\n",
" # return cv::arcLength(a, false) > cv::arcLength(b, false); });\n",
"\n",
" # int numContours = contours.size();\n",
"\n",
"\n",
" # return cv::boundingRect(contours[0]);"
]
}
],
"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.10.12"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}