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>
388 lines
11 KiB
Plaintext
388 lines
11 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 772,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import cv2\n",
|
|
"import numpy as np\n",
|
|
"\n",
|
|
"import myfunctions as mf\n",
|
|
"\n",
|
|
"\n",
|
|
"import scipy.stats as st\n",
|
|
"import math"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 773,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# read image as grayscale\n",
|
|
"img = cv2.imread('./test_images/IMG_7605.jpg')\n",
|
|
"# img = mf.ResizeWithAspectRatio(img,1000)\n",
|
|
"# img = mf.rotate(img, 54)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 774,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"prepped = mf.ResizeWithAspectRatio(mf.SquarePad(fill=255)(img),1000)\n",
|
|
"prepped = mf.premorphCrop(prepped)\n",
|
|
"prepped = mf.ResizeWithAspectRatio(mf.SquarePad(fill=255)(prepped),1000)\n",
|
|
"# kernel = np.ones((5,5), np.uint8)\n",
|
|
"# prepped = cv2.dilate(prepped, kernel, iterations=1)\n",
|
|
"gray1 = cv2.cvtColor(prepped, cv2.COLOR_BGR2GRAY)\n",
|
|
"dst1 = cv2.Canny(gray1, 0, 500, None, 3)\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",
|
|
"\n",
|
|
"dst1 = cv2.Canny(dst1, 0, 500, None, 3)\n",
|
|
"\n",
|
|
"cdstP = prepped.copy()\n",
|
|
"cdstPmargin = cdstP.copy()\n",
|
|
"basecdstP = cdstP.copy()\n",
|
|
"linesP = cv2.HoughLinesP(dst1, 1, np.pi / 180, 30, None, 90, 30)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 779,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# # testing = dst1.copy()\n",
|
|
"# # kernel = np.ones((5,5), np.uint8)\n",
|
|
"# # out = cv2.morphologyEx(testing, cv2.MORPH_DILATE, kernel)\n",
|
|
"# # out = cv2.blur(out, (5,5))\n",
|
|
"# # kernel = np.ones((3,3), np.uint8)\n",
|
|
"# # out = cv2.morphologyEx(out, cv2.MORPH_ERODE, kernel)\n",
|
|
"cv2.imshow(\"result1\", dst1)\n",
|
|
"cv2.waitKey(0)\n",
|
|
"cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 758,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"angles = np.zeros(len(linesP))\n",
|
|
"if linesP is not None:\n",
|
|
" for i in range(0, len(linesP)):\n",
|
|
" l = linesP[i][0]\n",
|
|
" angles[i] = mf.lineAngle(l)\n",
|
|
" cv2.line(cdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv2.LINE_AA)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 759,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# cv2.imshow(\"result1\", cdstP)\n",
|
|
"# cv2.waitKey(0)\n",
|
|
"# cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 760,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"-3.093972093706445\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"mode = st.mode(np.around(angles, decimals=3))[0]\n",
|
|
"rotationangle = np.rad2deg(mode)\n",
|
|
"print(rotationangle)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 761,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"rotatedcdstP = mf.rotate(basecdstP, rotationangle)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 762,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"vmarginlines = mf.WithinXDegrees(linesP, 7, baseangle=rotationangle)\n",
|
|
"hmarginlines = mf.WithinXDegrees(linesP, 7, baseangle=90+rotationangle)\n",
|
|
"vrect = mf.lineBoundingRect(vmarginlines,asRect=False, returnint=True)\n",
|
|
"hmarginlines = mf.lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)\n",
|
|
"\n",
|
|
"\n",
|
|
"if (hmarginlines != []):\n",
|
|
" marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
|
"else:\n",
|
|
" marginlines = vmarginlines\n",
|
|
" \n",
|
|
"rect = mf.lineBoundingRect(marginlines,asRect=False, returnint=True)\n",
|
|
"cdstP = cv2.rectangle(cdstP, (rect[0],rect[1]), (rect[2],rect[3]), (0,255,0), 3)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 763,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"cv2.imshow(\"result1\", cdstP)\n",
|
|
"cv2.waitKey(0)\n",
|
|
"cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 764,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#####NEED TO WORK ON SCORING THE LINES SO IT PICKS THE CORRECT ORIENTATION (horizontal vs vertical) AND SO THAT THE CROPPING RECTANGLE MOVES/GET TRANSFORMED WITH IT"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 780,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def rotatePoint(img, pt, angle, returnint=True):\n",
|
|
" rotateaxisx = img.shape[0]/2\n",
|
|
" rotateaxisy = img.shape[1]/2\n",
|
|
" tempx = pt[0] - rotateaxisx\n",
|
|
" tempy = pt[1] - rotateaxisy\n",
|
|
" rotatedx = tempx*math.cos(np.deg2rad(-angle)) - tempy*math.sin(np.deg2rad(-angle))\n",
|
|
" rotatedy = tempx*math.sin(np.deg2rad(-angle)) + tempy*math.cos(np.deg2rad(-angle))\n",
|
|
" finalx = rotatedx + rotateaxisx\n",
|
|
" finaly = rotatedy + rotateaxisy\n",
|
|
" if (returnint):\n",
|
|
" finalx = int(finalx)\n",
|
|
" finaly = int(finaly)\n",
|
|
" return (finalx, finaly)\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 766,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def rotateRect(img, rect, angle, returnint=True, asRect=False):\n",
|
|
" if (asRect):\n",
|
|
" pt1 = rotatePoint(img, (rect[0],rect[1]), angle, returnint)\n",
|
|
" pt2 = rotatePoint(img, (rect[0]+rect[2],rect[1]+rect[3]), angle, returnint)\n",
|
|
" return (pt1[0], pt1[1], pt2[0]-pt1[0], pt2[1]-pt1[1])\n",
|
|
" else:\n",
|
|
" pt1 = rotatePoint(img, (rect[0],rect[1]), angle, returnint)\n",
|
|
" pt2 = rotatePoint(img, (rect[2],rect[3]), angle, returnint)\n",
|
|
" return (pt1[0], pt1[1], pt2[0], pt2[1])\n",
|
|
"\n",
|
|
"def rotateLine(img, line, angle, returnint=True):\n",
|
|
" pt1 = rotatePoint(img, (line[0],line[1]), angle, returnint)\n",
|
|
" pt2 = rotatePoint(img, (line[2],line[3]), angle, returnint)\n",
|
|
" return (pt1[0], pt1[1], pt2[0], pt2[1])\n",
|
|
" \n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 767,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# print(linesP.shape)\n",
|
|
"rotatedlines = [rotateLine(rotatedcdstP, line[0], rotationangle) for line in linesP]\n",
|
|
"rotatedlines = np.reshape(rotatedlines, (len(rotatedlines),1,4))\n",
|
|
"# rotatedlines = linesP\n",
|
|
"# print(rotatedlines.shape)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 768,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"vmarginlines = mf.WithinXDegrees(rotatedlines, 7)\n",
|
|
"hmarginlines = mf.WithinXDegrees(rotatedlines, 7, baseangle=90)\n",
|
|
"vrect = mf.lineBoundingRect(vmarginlines,asRect=False, returnint=True)\n",
|
|
"hmarginlines = mf.lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)\n",
|
|
"\n",
|
|
"if (hmarginlines != []):\n",
|
|
" marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
|
"else:\n",
|
|
" marginlines = vmarginlines\n",
|
|
" \n",
|
|
"rect = mf.lineBoundingRect(marginlines,asRect=False, returnint=True)\n",
|
|
"# rect = vrect\n",
|
|
"rotatedcdstP = cv2.rectangle(rotatedcdstP, (rect[0],rect[1]), (rect[2],rect[3]), (0,255,0), 3)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 769,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if rotatedlines is not None:\n",
|
|
" for i in range(0, len(rotatedlines)):\n",
|
|
" l = rotatedlines[i][0]\n",
|
|
" cv2.line(rotatedcdstP, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv2.LINE_AA)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 771,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"cv2.imshow(\"result1\", rotatedcdstP)\n",
|
|
"# cv2.imshow(\"result1\", cdstP)\n",
|
|
"cv2.waitKey(0)\n",
|
|
"cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 394,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"vmarginlines = mf.WithinXDegrees(linesP, 7)\n",
|
|
"hmarginlines = mf.WithinXDegrees(linesP, 7, baseangle=90)\n",
|
|
"vrect = mf.lineBoundingRect(vmarginlines,asRect=False, returnint=True)\n",
|
|
"hmarginlines = mf.lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)\n",
|
|
"\n",
|
|
"\n",
|
|
"if (hmarginlines != []):\n",
|
|
" marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
|
"else:\n",
|
|
" marginlines = vmarginlines\n",
|
|
"\n",
|
|
"rect = mf.lineBoundingRect(marginlines,asRect=False, returnint=True)\n",
|
|
"cdstP = cv2.rectangle(cdstP, (rect[0],rect[1]), (rect[2],rect[3]), (0,255,0), 3)\n",
|
|
"\n",
|
|
"\n",
|
|
"# rotatedrect = rotateRect(cdstP, rect, -rotationangle)\n",
|
|
"\n",
|
|
"# rotatedcdstP = cv2.rectangle(rotatedcdstP, (rotatedrect[0],rotatedrect[1]), (rotatedrect[2],rotatedrect[3]), (0,255,0), 3)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 395,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"###figure out how to rotate rectangle"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 396,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"cv2.imshow(\"result1\", cdstP)\n",
|
|
"cv2.waitKey(0)\n",
|
|
"cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 397,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# vmarginlines = mf.WithinXDegrees(linesP, 7)\n",
|
|
"# hmarginlines = mf.WithinXDegrees(linesP, 7, baseangle=90)\n",
|
|
"# vrect = mf.lineBoundingRect(vmarginlines,asRect=False, returnint=True)\n",
|
|
"# hmarginlines = mf.lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)\n",
|
|
"# # print(hmarginlines)\n",
|
|
"# if (hmarginlines != []):\n",
|
|
"# marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
|
"# else:\n",
|
|
"# marginlines = vmarginlines\n",
|
|
"\n",
|
|
"# # print(marginlines)\n",
|
|
"# rect = mf.lineBoundingRect(marginlines,asRect=False, returnint=True)\n",
|
|
"# # print(rect)\n",
|
|
"# cdstP = cv2.rectangle(cdstP, (rect[0],rect[1]), (rect[2],rect[3]), (0,255,0), 3)\n",
|
|
"# # print(cdstP.shape)\n",
|
|
"# # cropped = cdstP[rect[1]:rect[3], rect[0]:rect[2],:]\n",
|
|
"\n",
|
|
"# if marginlines is not None:\n",
|
|
"# for i in range(0, len(marginlines)):\n",
|
|
"# l = marginlines[i]\n",
|
|
"# cv2.line(cdstP, (int(l[0]), int(l[1])), (int(l[2]), int(l[3])), (255,0,0), 3, cv2.LINE_AA)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 398,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# # view result\n",
|
|
"# # cv2.imshow(\"threshold\", thresh)\n",
|
|
"# # cv2.imshow(\"morph\", morph)\n",
|
|
"# # cv2.imshow(\"mask\", mask)\n",
|
|
"# cv2.imshow(\"result1\", mf.ResizeWithAspectRatio(cdstP,height=1000))\n",
|
|
"# # cv2.imshow(\"result2\", cropped)\n",
|
|
"# cv2.waitKey(0)\n",
|
|
"# cv2.destroyAllWindows()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3 (ipykernel)",
|
|
"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"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|