Random whiteout checkpoint
Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
This commit is contained in:
parent
d9aa07121a
commit
6a27931e8b
@ -2,20 +2,9 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 1895,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stderr",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/usr/local/lib/python3.10/dist-packages/torchvision/datapoints/__init__.py:12: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning().\n",
|
||||
" warnings.warn(_BETA_TRANSFORMS_WARNING)\n",
|
||||
"/usr/local/lib/python3.10/dist-packages/torchvision/transforms/v2/__init__.py:54: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning().\n",
|
||||
" warnings.warn(_BETA_TRANSFORMS_WARNING)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import cv2\n",
|
||||
"import myfunctions as mf\n",
|
||||
@ -26,7 +15,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 1896,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -66,7 +55,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 1897,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -88,7 +77,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 1898,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -102,18 +91,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 1899,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"img = cv2.imread('/mnt/dataset/baseimages/6.jpg')\n",
|
||||
"img = cv2.imread('/mnt/dataset/baseimages/12.jpg')\n",
|
||||
"# img = cv2.imread('/mnt/code/autocropper/test_images/IMG_7605.jpg')\n",
|
||||
"testall = False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 1900,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -121,52 +110,209 @@
|
||||
"\n",
|
||||
"\n",
|
||||
"def whiteoutbackground(image):\n",
|
||||
" sdim = int(min(image.shape[0], image.shape[1])/10)\n",
|
||||
" ogshape = image.shape\n",
|
||||
" shrunkdim=1000\n",
|
||||
" if (image.shape[1] > image.shape[0]):\n",
|
||||
" shrunkimg, scaler = mf.ResizeWithAspectRatio(image, width=shrunkdim, retscale=True)\n",
|
||||
" else:\n",
|
||||
" shrunkimg, scaler = mf.ResizeWithAspectRatio(image, height=shrunkdim, retscale=True)\n",
|
||||
" \n",
|
||||
" mainimage = shrunkimg\n",
|
||||
" \n",
|
||||
" sdim = int(min(mainimage.shape[0], mainimage.shape[1])/5)\n",
|
||||
" srkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sdim, sdim))\n",
|
||||
" skernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (sdim, sdim))\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)\n",
|
||||
" lab = cv2.cvtColor(mainimage, cv2.COLOR_BGR2LAB)\n",
|
||||
" \n",
|
||||
" imglist = []\n",
|
||||
" imglist.append(image)\n",
|
||||
" # imglist.append(mainimage)\n",
|
||||
" \n",
|
||||
" # currentimgofatype = lab[:,:,0]\n",
|
||||
" # imglist.append(currentimgofatype)\n",
|
||||
" labl = lab[:,:,0]\n",
|
||||
" # imglist.append(labl)\n",
|
||||
" # imglist.append(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))\n",
|
||||
" laba = lab[:,:,1]\n",
|
||||
" # imglist.append(currentimgofatype)\n",
|
||||
" # imglist.append(laba)\n",
|
||||
" labb = lab[:,:,2]\n",
|
||||
" # imglist.append(currentimgofatype)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" blurimage1 = cv2.blur(laba, (5,5))\n",
|
||||
" blurimage2 = cv2.blur(labb, (5,5))\n",
|
||||
" # imglist.append(labb)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # canny = cv2.Canny(labl, 0, 500)\n",
|
||||
" threshl = cv2.threshold(labl, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
||||
" # return threshl\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" dim = int(min(mainimage.shape[0], mainimage.shape[1])/100)\n",
|
||||
" # dim = 2\n",
|
||||
" # dim = dotsize\n",
|
||||
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (dim, dim))\n",
|
||||
" kernelell = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dim, dim))\n",
|
||||
" \n",
|
||||
" paddedl = mf.padWithColour(threshl, sdim*2, sdim*2, fill=0)\n",
|
||||
" # return paddedl\n",
|
||||
" \n",
|
||||
" morphedl = paddedl\n",
|
||||
" morphedl = cv2.morphologyEx(morphedl, cv2.MORPH_OPEN, kernel, iterations=1)\n",
|
||||
" \n",
|
||||
" # return morphedl\n",
|
||||
" \n",
|
||||
" canny = cv2.Canny(morphedl, 0, 500)\n",
|
||||
" # return canny\n",
|
||||
"\n",
|
||||
" minlength = max(mainimage.shape[0], mainimage.shape[1])//7\n",
|
||||
" mingap = max(mainimage.shape[0], mainimage.shape[1])//20\n",
|
||||
" linesP = cv2.HoughLinesP(canny, 1, np.pi / 180, 30, None, minlength, mingap)\n",
|
||||
" # print(linesP)\n",
|
||||
" \n",
|
||||
" vmarginlines = mf.WithinXDegrees(linesP, 10)\n",
|
||||
" hmarginlines = mf.WithinXDegrees(linesP, 10, baseangle=90)\n",
|
||||
" \n",
|
||||
" marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
||||
" \n",
|
||||
" # colourdst = cv2.cvtColor(morphedl, cv2.COLOR_GRAY2BGR)\n",
|
||||
" # if marginlines is not None:\n",
|
||||
" # for l in marginlines:\n",
|
||||
" # cv2.line(colourdst, (int(l[0]), int(l[1])), (int(l[2]), int(l[3])), (0,0,255), 3, cv2.LINE_AA)\n",
|
||||
" # return colourdst\n",
|
||||
"\n",
|
||||
" # morphedl = 255-cv2.morphologyEx(255-threshl, cv2.MORPH_OPEN, kernel, iterations=3)\n",
|
||||
" morphedl = paddedl\n",
|
||||
" morphedl = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
||||
" morphedl = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernelell, iterations=1)\n",
|
||||
"\n",
|
||||
" # return morphedl\n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(morphedl, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
" print(contours)\n",
|
||||
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
||||
" return canny\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" blank = np.full(labl.shape, 255, dtype=np.uint8)\n",
|
||||
" mask1 = blank.copy()\n",
|
||||
" mask1 = mf.padWithColour(mask1, sdim*2, sdim*2, fill=255)\n",
|
||||
" mask1 = cv2.drawContours(mask1, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" mask1 = mask1[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # resizemask = cv2.resize(mask1, (ogshape[1], ogshape[0]))\n",
|
||||
" # return resizemask\n",
|
||||
" maskc = cv2.cvtColor(mask1, cv2.COLOR_GRAY2BGR)\n",
|
||||
" # print(maskc.shape)\n",
|
||||
" # print(image.shape)\n",
|
||||
" whitedbackground = cv2.bitwise_or(mainimage, maskc)\n",
|
||||
" # return whitedbackground\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" lab2 = cv2.cvtColor(whitedbackground, cv2.COLOR_BGR2LAB)\n",
|
||||
" \n",
|
||||
" lab2l = lab2[:,:,0]\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" otsu2 = cv2.threshold(lab2l, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
||||
" \n",
|
||||
" expandedmask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, kernel, iterations=1)\n",
|
||||
" expandedmask1 = cv2.morphologyEx(expandedmask1, cv2.MORPH_DILATE, kernelell, iterations=1)\n",
|
||||
" # return expandedmask1\n",
|
||||
" \n",
|
||||
" maskmerge = cv2.bitwise_and(otsu2, 255-expandedmask1)\n",
|
||||
" return mask1\n",
|
||||
" return maskmerge\n",
|
||||
" \n",
|
||||
" # return otsu2\n",
|
||||
" \n",
|
||||
" mpad = mf.padWithColour(maskmerge, sdim*2, sdim*2, fill=0)\n",
|
||||
" return mpad\n",
|
||||
" \n",
|
||||
" #MORPHOLOGIES \n",
|
||||
" morphed2 = cv2.morphologyEx(mpad, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
||||
" morphed2 = cv2.morphologyEx(morphed2, cv2.MORPH_ERODE, kernelell, iterations=1)\n",
|
||||
" return morphed2\n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(morphed2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" mask2 = blank.copy()\n",
|
||||
" mask2 = mf.padWithColour(mask2, sdim*2, sdim*2, fill=255)\n",
|
||||
" mask2 = cv2.drawContours(mask2, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" mask2 = mask2[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
||||
" \n",
|
||||
" return mask2\n",
|
||||
" \n",
|
||||
" test = cv2.inpaint(whitedbackground, resizemask, 3, cv2.INPAINT_TELEA)\n",
|
||||
" \n",
|
||||
" return test\n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(255-labl, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
" \n",
|
||||
" imgout = cv2.drawContours(mainimage, contours, -1, (0,255,0), thickness=3)\n",
|
||||
" return imgout\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
"\n",
|
||||
"\n",
|
||||
" blurimagel = cv2.blur(labl, (7,7))\n",
|
||||
" blurimagea = cv2.blur(laba, (7,7))\n",
|
||||
" blurimageb = cv2.blur(labb, (7,7))\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" window = lab.shape[1]//5\n",
|
||||
" if window % 2 == 0:\n",
|
||||
" window += 1\n",
|
||||
" threshl = 255-cv2.adaptiveThreshold(blurimagel, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 2)\n",
|
||||
" window = lab.shape[1]//3\n",
|
||||
" if window % 2 == 0:\n",
|
||||
" window += 1\n",
|
||||
" thresh1 = cv2.adaptiveThreshold(blurimage1, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 0)\n",
|
||||
" thresha = cv2.adaptiveThreshold(blurimagea, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 0)\n",
|
||||
" window = lab.shape[1]//10\n",
|
||||
" if window % 2 == 0:\n",
|
||||
" window += 1\n",
|
||||
" thresh2 = cv2.adaptiveThreshold(blurimage2, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 0)\n",
|
||||
" # return thresh1\n",
|
||||
" imglist.append(thresh1)\n",
|
||||
" imglist.append(thresh2)\n",
|
||||
" threshmerge = cv2.bitwise_or(thresh1, thresh2)\n",
|
||||
" imglist.append(threshmerge)\n",
|
||||
" threshb = cv2.adaptiveThreshold(blurimageb, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 0)\n",
|
||||
" # return threshl\n",
|
||||
" # imglist.append(threshl)\n",
|
||||
" # imglist.append(thresha)\n",
|
||||
" # imglist.append(threshb)\n",
|
||||
" # threshmerge = cv2.bitwise_or(thresha, threshb)\n",
|
||||
" # imglist.append(threshmerge)\n",
|
||||
" # threshmerge = cv2.bitwise_or(threshl, threshmerge)\n",
|
||||
" # imglist.append(threshmerge)\n",
|
||||
" # return imglist\n",
|
||||
" # return threshmerge\n",
|
||||
" threshmerge = threshl\n",
|
||||
" \n",
|
||||
" dim = int(min(threshmerge.shape[0], threshmerge.shape[1])/100)\n",
|
||||
" # dim = 2\n",
|
||||
" # dim = dotsize\n",
|
||||
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (dim, dim))\n",
|
||||
" \n",
|
||||
" padthresh = mf.padWithColour(threshmerge, sdim, sdim, fill=255)\n",
|
||||
" padthresh = mf.padWithColour(threshmerge, sdim*2, sdim*2, fill=255)\n",
|
||||
" mask1t = cv2.morphologyEx(padthresh, cv2.MORPH_OPEN, kernel, iterations=1)\n",
|
||||
" # mask1t = cv2.morphologyEx(mask1t, cv2.MORPH_OPEN, skernel)\n",
|
||||
" imglist.append(mask1t)\n",
|
||||
" # imglist.append(mask1t)\n",
|
||||
" # return mask1t\n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(255-mask1t, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
@ -174,27 +320,77 @@
|
||||
" \n",
|
||||
" blank = np.full(threshmerge.shape, 255, dtype=np.uint8)\n",
|
||||
" mask1 = blank.copy()\n",
|
||||
" mask1 = mf.padWithColour(mask1, sdim, sdim, fill=255)\n",
|
||||
" mask1 = mf.padWithColour(mask1, sdim*2, sdim*2, fill=255)\n",
|
||||
" mask1 = cv2.drawContours(mask1, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
||||
" # temp = cv2.drawContours(image, [biggestcontour], -1, 255, thickness=3)\n",
|
||||
" # imglist.append(temp)\n",
|
||||
" imglist.append(mask1)\n",
|
||||
" # imglist.append(mask1)\n",
|
||||
" # return mask1\n",
|
||||
" \n",
|
||||
" mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, skernel, iterations=2)\n",
|
||||
" # mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, skernel, iterations=2)\n",
|
||||
" mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, skernel)\n",
|
||||
" mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, skernel, iterations=1)\n",
|
||||
" # return mask1\n",
|
||||
" # mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, skernel, iterations=2)\n",
|
||||
" mask1 = mask1[sdim:-sdim, sdim:-sdim]\n",
|
||||
" imglist.append(mask1)\n",
|
||||
" mask1 = cv2.morphologyEx(mask1, cv2.MORPH_OPEN, skernel, iterations=1)\n",
|
||||
" # mask1 = cv2.morphologyEx(mask1, cv2.MORPH_CLOSE, skernel, iterations=2)\n",
|
||||
" mask1 = mask1[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
||||
" # imglist.append(mask1)\n",
|
||||
" # return mask1\n",
|
||||
" \n",
|
||||
" maskc = cv2.cvtColor(mask1, cv2.COLOR_GRAY2BGR)\n",
|
||||
" whitedbackground = cv2.bitwise_or(image, maskc)\n",
|
||||
" imglist.append(whitedbackground)\n",
|
||||
" # mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, kernel)\n",
|
||||
" \n",
|
||||
" # resizemask = cv2.resize(mask1, (ogshape[1], ogshape[0]))\n",
|
||||
" resizemask = mask1\n",
|
||||
" # return resizemask\n",
|
||||
" maskc = cv2.cvtColor(resizemask, cv2.COLOR_GRAY2BGR)\n",
|
||||
" # print(maskc.shape)\n",
|
||||
" # print(image.shape)\n",
|
||||
" whitedbackground = cv2.bitwise_or(shrunkimg, maskc)\n",
|
||||
" # imglist.append(whitedbackground)\n",
|
||||
" # return whitedbackground\n",
|
||||
" \n",
|
||||
" test = cv2.inpaint(whitedbackground, resizemask, 3, cv2.INPAINT_TELEA)\n",
|
||||
" # return test\n",
|
||||
" # if (test.shape[1] > test.shape[0]):\n",
|
||||
" # shrunktest = mf.ResizeWithAspectRatio(image, width=shrunkdim)\n",
|
||||
" # else:\n",
|
||||
" # shrunktest = mf.ResizeWithAspectRatio(image, height=shrunkdim)\n",
|
||||
" \n",
|
||||
" sdim = int(min(mainimage.shape[0], mainimage.shape[1])/40)\n",
|
||||
" srkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sdim, sdim))\n",
|
||||
" skernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (sdim, sdim))\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" lab2 = cv2.cvtColor(test, cv2.COLOR_BGR2LAB)\n",
|
||||
" lab2l = lab2[:,:,0]\n",
|
||||
" lab2a = lab2[:,:,1]\n",
|
||||
" lab2b = lab2[:,:,2]\n",
|
||||
" blurimage2l = cv2.blur(lab2l, (3,3))\n",
|
||||
" # blurimage2l = lab2l\n",
|
||||
" \n",
|
||||
" # window = lab.shape[1]//10\n",
|
||||
" # if window % 2 == 0:\n",
|
||||
" # window += 1\n",
|
||||
" # thresh2l = 255-cv2.adaptiveThreshold(blurimage2l, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 0)\n",
|
||||
" thresh2l = cv2.threshold(blurimage2l, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
||||
" \n",
|
||||
" morphed2l = 255-cv2.morphologyEx(255-thresh2l, cv2.MORPH_OPEN, kernel, iterations=3)\n",
|
||||
" padded2l = mf.padWithColour(morphed2l, sdim*2, sdim*2, fill=0)\n",
|
||||
" \n",
|
||||
" mask2 = cv2.morphologyEx(padded2l, cv2.MORPH_OPEN, srkernel)\n",
|
||||
" # mask2 = padded2l\n",
|
||||
" \n",
|
||||
" mask2 = 255-mask2[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
||||
" \n",
|
||||
" ##IT'S GOOD HERE. NOW, I CAN IMPLEMENT THE SHRINKING AGAIN, ADD PADDING, MORPHOLOGY TO CONNECT THE SHIT TO THE EDGE, AND THEN GET THE BIGGEST CONTOUR AGAIN\n",
|
||||
" ## shrinking part \"implemented\" (it wasn't expanded to begin with), the padding and morphology+ still needs to be done\n",
|
||||
" # return mask2\n",
|
||||
"\n",
|
||||
" mask2c = cv2.cvtColor(mask2, cv2.COLOR_GRAY2BGR)\n",
|
||||
" whitedbackground = cv2.bitwise_or(test, mask2c)\n",
|
||||
" return whitedbackground\n",
|
||||
" \n",
|
||||
" \n",
|
||||
"\n",
|
||||
" \n",
|
||||
" return imglist\n",
|
||||
" \n",
|
||||
" \n",
|
||||
@ -459,7 +655,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 1901,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -479,230 +675,13 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def prepimageforhoughline(image, returnrect=True):\n",
|
||||
" prepped, scaler, hp, vp = mf.squareandthenresize(image, fill=255, width=1000, returnscalerinfo=True)\n",
|
||||
" ogpreppedshape = prepped.shape\n",
|
||||
" # print(ogpreppedshape)\n",
|
||||
" prepped, croprect = mf.premorphCrop(prepped)\n",
|
||||
" # print(prepped.shape)\n",
|
||||
" if (prepped.shape[1] > prepped.shape[0]):\n",
|
||||
" prepped, preppedscaler = mf.ResizeWithAspectRatio(prepped, width=1000, retscale=True)\n",
|
||||
" else:\n",
|
||||
" prepped, preppedscaler = mf.ResizeWithAspectRatio(prepped, height=1000, retscale=True)\n",
|
||||
" # print(prepped.shape)\n",
|
||||
" # print(preppedscaler)\n",
|
||||
" finalcroprect = (int(croprect[0]*scaler - hp), int(croprect[1]*scaler - vp), int(croprect[2]*scaler), int(croprect[3]*scaler))\n",
|
||||
" gray1 = cv2.cvtColor(prepped, cv2.COLOR_BGR2GRAY)\n",
|
||||
"\n",
|
||||
" dst1 = cv2.Canny(gray1, 0, 500, None, 3)\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" kernel = np.ones((5,5), np.uint8)\n",
|
||||
" out = cv2.morphologyEx(dst1, cv2.MORPH_DILATE, kernel)\n",
|
||||
" out = cv2.blur(out, (5,5))\n",
|
||||
" kernel = np.ones((6,6), np.uint8)\n",
|
||||
" dst1 = cv2.morphologyEx(out, cv2.MORPH_ERODE, kernel)\n",
|
||||
" # return dst1\n",
|
||||
"\n",
|
||||
" dst1 = cv2.Canny(dst1, 0, 500, None, 3)\n",
|
||||
" # return dst1\n",
|
||||
" accompaniedimage = image[finalcroprect[1]:finalcroprect[1]+finalcroprect[3], finalcroprect[0]:finalcroprect[0]+finalcroprect[2], :]\n",
|
||||
" if returnrect:\n",
|
||||
" borderType = cv2.BORDER_CONSTANT\n",
|
||||
" ## first the og padding when we include the padding added by squaring\n",
|
||||
" preppadding = [croprect[0], croprect[1], ogpreppedshape[1]-(croprect[0]+croprect[2]), ogpreppedshape[0]-(croprect[1]+croprect[3])]\n",
|
||||
" # print(croprect)\n",
|
||||
" # print(dst1.shape)\n",
|
||||
" # print(preppadding)\n",
|
||||
" preppadding = [int(s/preppedscaler) for s in preppadding]\n",
|
||||
" # print(preppadding)\n",
|
||||
" ## now adjust for any padding. hp and vp are for the full sized image so they need to be scaled down first.\n",
|
||||
" # adjustedhp = int(hp/scaler)\n",
|
||||
" # adjustedvp = int(vp/scaler)\n",
|
||||
" # preppadding[0] -= adjustedhp\n",
|
||||
" # preppadding[2] -= adjustedhp\n",
|
||||
" # preppadding[1] -= adjustedvp\n",
|
||||
" # preppadding[3] -= adjustedvp\n",
|
||||
" \n",
|
||||
" paddedprepped = cv2.copyMakeBorder(dst1, preppadding[1], preppadding[3], preppadding[0], preppadding[2], borderType, 0)\n",
|
||||
" # paddedprepped = dst1\n",
|
||||
" squaredimage = mf.squarepad(image, fill=0)\n",
|
||||
" return dst1, accompaniedimage, paddedprepped, squaredimage, finalcroprect\n",
|
||||
" else:\n",
|
||||
" # accompaniedimage = squarepad(accompaniedimage, fill=255)\n",
|
||||
" return dst1, accompaniedimage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlinedeskewthencrop(baseimage, preppedimage, rotationangle, croprect):\n",
|
||||
" # imglist = []\n",
|
||||
" # print(preppedimage.shape)\n",
|
||||
" # print(baseimage.shape)\n",
|
||||
" \n",
|
||||
" # imgcopy = baseimage.copy()\n",
|
||||
" # sizemultiplier = croprect[3]/preppedimage.shape[0]\n",
|
||||
" \n",
|
||||
" ##adjust the rotation angle if it causes the rectangle to flip w and h size ordering. That is, if it will cause the width to be greater than the height or something. let me think about it for a second more.\n",
|
||||
" # print(baseimage.shape[:2])\n",
|
||||
" # mask = np.full(baseimage.shape[:2], fill_value=255, dtype=np.uint8)\n",
|
||||
" # mask = cv2.rectangle(mask, (croprect[0], croprect[1]), (croprect[0]+croprect[2], croprect[1]+croprect[3]), color=0, thickness=cv2.FILLED)\n",
|
||||
" # temp = baseimage[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2], :]\n",
|
||||
" \n",
|
||||
" # rotatedmask = mf.rotate(mask, rotationangle, fill=255)\n",
|
||||
" # print(mask.shape)\n",
|
||||
" # return mask, 5\n",
|
||||
" # rotatedmask = mf.rotatewithexactpadding(mask, rotationangle, fill=255)\n",
|
||||
" rotatedbaseimage = mf.rotatewithexactpadding(baseimage, rotationangle, fill=(0,0,0))\n",
|
||||
" # rotatedtemp = mf.rotatewithexactpadding(temp, rotationangle, fill=(0,0,0))\n",
|
||||
" # return preppedimage, 5\n",
|
||||
" rotateddst1 = mf.rotatewithexactpadding(preppedimage, rotationangle, fill=(0,0,0))\n",
|
||||
" \n",
|
||||
" sizemultiplier = rotatedbaseimage.shape[0]/rotateddst1.shape[0]\n",
|
||||
" \n",
|
||||
" # fcr = mf.croptoblack(rotatedmask, extraborder=0, returnrect=True) #finalcroprect\n",
|
||||
" \n",
|
||||
" # print(sizemultiplier)\n",
|
||||
" # imglist.append(rotatedmask)\n",
|
||||
" # imglist.append(rotatedbaseimage)\n",
|
||||
" # imglist.append(rotateddst1)\n",
|
||||
" # return imglist, 5\n",
|
||||
" # return rotatedmask, 5\n",
|
||||
" # return rotatedbaseimage, 5\n",
|
||||
" # return rotateddst1, 5\n",
|
||||
" # print(fcr)\n",
|
||||
" # rotatedbaseimage = rotatedbaseimage[fcr[1]:fcr[1]+fcr[3], fcr[0]:fcr[0]+fcr[2]]\n",
|
||||
" # rotateddst1 = rotateddst1[fcr[1]:fcr[1]+fcr[3], fcr[0]:fcr[0]+fcr[2]]\n",
|
||||
" # return mask, 5\n",
|
||||
" # return rotatedbaseimage, 5\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # sizemultiplier = rotatedbaseimage.shape[0]/rotateddst1.shape[0]\n",
|
||||
" # print(sizemultiplier)\n",
|
||||
" # return rotatedbaseimage, rotationangle\n",
|
||||
"\n",
|
||||
" croppedbaseimage = mf.houghlinepcrop(rotatedbaseimage, rotateddst1, sizemultiplier)\n",
|
||||
" \n",
|
||||
" # return croppedbaseimage, rotationangle\n",
|
||||
"\n",
|
||||
" finalbaseimage = mf.contourcrop(croppedbaseimage)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" return finalbaseimage, rotationangle\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # rotateddst1 = rotatewithexactpadding(preppedimage, rotationangle, fill=0)\n",
|
||||
" # rotatedbaseimage = rotatewithexactpadding(baseimage, rotationangle, fill=(0,0,0))\n",
|
||||
" # print(croprect)\n",
|
||||
" hpad, vpad = mf.determineextrapadding(croprect[3], croprect[2], rotationangle)\n",
|
||||
" # print(rotationangle)\n",
|
||||
" # print(hpad, vpad)\n",
|
||||
" adjustedrect = [croprect[0]-hpad, croprect[1]-vpad, croprect[2]+(2*hpad), croprect[3]+(2*vpad)]\n",
|
||||
" adjustedrect = [int(s) for s in adjustedrect]\n",
|
||||
"\n",
|
||||
" # print(adjustedrect)\n",
|
||||
" leftpad = abs(min(0, adjustedrect[0]))\n",
|
||||
" toppad = abs(min(0, adjustedrect[1]))\n",
|
||||
" rightpad = abs(min(0, baseimage.shape[1]-(adjustedrect[0]+adjustedrect[2])))\n",
|
||||
" bottompad = abs(min(0, baseimage.shape[0]-(adjustedrect[1]+adjustedrect[3])))\n",
|
||||
" \n",
|
||||
" # print(leftpad, rightpad, toppad, bottompad)\n",
|
||||
" \n",
|
||||
" borderType = cv2.BORDER_CONSTANT\n",
|
||||
" # padded = cv2.copyMakeBorder(baseimage, toppad, bottompad, leftpad, rightpad, borderType)\n",
|
||||
" # imgcopy = padded.copy()\n",
|
||||
" imgcopy = cv2.rectangle(imgcopy, (croprect[0], croprect[1]), (croprect[0]+croprect[2], croprect[1]+croprect[3]), color=(0,255,0), thickness=3)\n",
|
||||
" imgcopy = cv2.rectangle(imgcopy, (adjustedrect[0], adjustedrect[1]), (adjustedrect[0]+adjustedrect[2], adjustedrect[1]+adjustedrect[3]), color=(0,0,255), thickness=3)\n",
|
||||
" \n",
|
||||
" adjustedrect = [max(0, s) for s in adjustedrect]\n",
|
||||
" imgcopy = cv2.rectangle(imgcopy, (adjustedrect[0], adjustedrect[1]), (adjustedrect[0]+adjustedrect[2], adjustedrect[1]+adjustedrect[3]), color=(255,0,0), thickness=3)\n",
|
||||
" return imgcopy, 5\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" ## DETERMINE PADDING AMOUNTS BECAUSE I THEN NEED TO PAD THE ORIGINAL IMAGE, CROP IT TO THE FIRST LEVEL, ROTATE, AND THEN CROP AGAIN\n",
|
||||
" # print(adjustedrect)\n",
|
||||
" # adjustedrect = croprect\n",
|
||||
" # borderType = cv2.BORDER_CONSTANT\n",
|
||||
" # padded = cv2.copyMakeBorder(baseimage, toppad, bottompad, leftpad, rightpad, borderType)\n",
|
||||
" croppedpaddedimage = padded[adjustedrect[1]:adjustedrect[1]+adjustedrect[3], adjustedrect[0]:adjustedrect[0]+adjustedrect[2], :]\n",
|
||||
" # print(croprect[2], croprect[3])\n",
|
||||
" # print(croppedpaddedimage.shape)\n",
|
||||
" # croppedog = baseimage[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2], :]\n",
|
||||
" # return croppedpaddedimage, 5\n",
|
||||
" \n",
|
||||
" rotatedbaseimage = mf.rotate(croppedpaddedimage, rotationangle)\n",
|
||||
" \n",
|
||||
" # print(vpad, hpad)\n",
|
||||
" # print(croprect)\n",
|
||||
" # rotatedbaseimage = rotatedbaseimage[vpad:vpad+croprect[3], hpad:hpad+croprect[2], :]\n",
|
||||
" return rotatedbaseimage, 5\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # print(preppedimage.shape)\n",
|
||||
" rotateddst1 = mf.rotate(preppedimage, rotationangle)\n",
|
||||
" # print(rotateddst1.shape)\n",
|
||||
" # print(rotatedbaseimage.shape)\n",
|
||||
" # return rotatedbaseimage, 5\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" sizemultiplier = rotatedbaseimage.shape[0]/rotateddst1.shape[0]\n",
|
||||
" # print(sizemultiplier)\n",
|
||||
" # return rotatedbaseimage, rotationangle\n",
|
||||
"\n",
|
||||
" croppedbaseimage = mf.houghlinepcrop(rotatedbaseimage, rotateddst1, sizemultiplier)\n",
|
||||
"\n",
|
||||
" finalbaseimage = mf.contourcrop(croppedbaseimage)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" return finalbaseimage, rotationangle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlinedeskewandcrop(image):\n",
|
||||
" croppedcanny, croppedimage, canny, ogimage, rect = prepimageforhoughline(image, returnrect=True) ## scaling and cropping occurs. need to also return the changes done\n",
|
||||
" # return canny, ogimage\n",
|
||||
" # print(canny.shape)\n",
|
||||
" # print(croppedogimage.shape)\n",
|
||||
"\n",
|
||||
" ## -----------------finding angle to deskew-----------------\n",
|
||||
" rotationangle = mf.houghlinedeskewangle(croppedcanny)\n",
|
||||
" # print(rotationangle)\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" # rotatorrect = findcroprectforangle(rect, angle)\n",
|
||||
"\n",
|
||||
" # -----------------end of finding angle to deskew-----------------\n",
|
||||
"\n",
|
||||
" ## -----------------deskewing and then cropping-----------------\n",
|
||||
" outimg, angle = houghlinedeskewthencrop(ogimage, canny, rotationangle, rect)\n",
|
||||
" return outimg, angle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 1902,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlineprocessing(image):\n",
|
||||
" croppedanddeskewed, angle = mf.houghlinedeskewandcrop(image)\n",
|
||||
" return croppedanddeskewed\n",
|
||||
" # return croppedanddeskewed\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" postprocessed = cropclarifying(croppedanddeskewed)\n",
|
||||
@ -727,7 +706,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 1903,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -736,7 +715,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 1904,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -752,7 +731,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 1905,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -766,7 +745,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 1906,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -778,7 +757,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 1907,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -787,7 +766,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 1908,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -797,7 +776,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 1909,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -827,14 +806,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": 1910,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"average time: 0.1962958723306656(s)\n"
|
||||
"average time: 0.20301511883735657(s)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -845,7 +824,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 1911,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -855,7 +834,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 21,
|
||||
"execution_count": 1912,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -864,7 +843,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 22,
|
||||
"execution_count": 1913,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
||||
Loading…
Reference in New Issue
Block a user