{ "info": { "author": "Andreas Bergem", "author_email": "bergem.andreas@gmail.com", "bugtrack_url": null, "classifiers": [ "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", "Programming Language :: Python :: 3.7", "Topic :: Scientific/Engineering :: Image Recognition" ], "description": "# Simple helper package for opencv-python\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/ambv/black)\n\ncvhelper is a simpler wrapper for the opencv-python package. As mentioned package only\ngives access to OpenCV functions, in a C++ style, it can be tedious to write. There is\nalso no support for the OpenCV classes like Rect, Point etc. cvhelper attempts to fix that.\n\nThe package is at an early state, and contributions are welcome! The contents of the package\nhave been a demand-and-supply model, where functionality is added as new tedious things in\nopencv-python are found. Do not hesitate to file an issue, requesting new functionality or \nenhancement proposals! \n\n## Installation\nInstallation is by the python package manager, pip. \n```bash\npip install cvhelper\n```\nThis also installs the dependencies `opencv-python`, `opencv-contrib-python` and `numpy`, if not already present.\n\n## Examples\n### Reading videos\nThis code speaks for itself.\n\nVanilla OpenCV:\n```python\nimport cv2 as cv\nvideo = cv.VideoCapture(\"path/to/file\")\nif not video.isOpened():\n raise ValueError(\"Could not open video\")\n\nwhile True:\n ok, frame = video.read()\n if not ok:\n break\n cv.imshow(\"Frame\", frame)\n if cv.waitKey(0) & 0xFF == ord('q'):\n break \nvideo.release()\n``` \n\ncvhelper:\n```python\nimport cv2 as cv\nimport cvhelper as cvh\nwith cvh.load_video(\"path/to/file\") as video:\n for frame in cvh.read_frames(video, start, stop, step):\n cv.imshow(\"Frame\", frame)\n if cvh.wait_key(0) == ord('q'):\n break \n```\n\n### Rotate A Color Wheel\nSay we have the following color wheel image, which we want to rotate.\n\n![alt text](https://raw.githubusercontent.com/anbergem/cvhelper/master/images/color_wheel.png)\n\nWe of course want to rotate it at it's center, which is not in the center\nof the image. A possible solution using OpenCV would be \n\n```python\nimport cv2 as cv\nimport random\n\nimg = cv.imread(\"resources/color_wheel_invert.png\")\ngray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)\n_, otsu = cv.threshold(gray, 250, 255, cv.THRESH_BINARY_INV)\n_, contours, _ = cv.findContours(otsu, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)\ncontour = contours[0]\nrect = cv.boundingRect(contour) # Gives a tuple (x, y, w, h)\nx, y, w, h = rect\n\ncolor = [random.randint(0, 255) for _ in range(3)]\n\ndegrees = 60\ncenter = (x + w / 2), (y + h / 2)\nrotation_matrix = cv.getRotationMatrix2D(center, degrees, 1)\nrotated_image = cv.warpAffine(img, rotation_matrix, gray.shape[::-1])\n\ncv.rectangle(rotated_image, (x, y), (x + w, y + h), color)\n\ncv.imshow(\"Image\", rotated_image)\ncv.waitKey(0)\n```\nWe first convert the image to gray scale. The color wheel in gray scale does not \ncontain any values of pure white. We can therefore threshold the image at a high\nthreshold, to segment the color wheel. \n\nWe then find contours in the image (which in this case only will be one contour), and\nfind the bounding rectangle enclosing the contour. From this rectangle we can find the center\npoint by the means of the top left corner, the height and width. We use this to create\na rotation matrix, and call the affine warp function. Lastly, we draw a rectangle around\nthe found contour. This is just for viewing pruposes.\n\nWe get the following result.\n\n![alt text](https://raw.githubusercontent.com/anbergem/cvhelper/master/images/helper.png)\n\nAlthough a perfectly fine solution, we cannot help but rotate the whole image.\nHere is a solution using cvhelper.\n\ncvhelper:\n```python\nimport cv2 as cv\nimport cvhelper as cvh\n\nimg = cv.imread(\"resources/color_wheel_invert.png\")\ngray = cvh.bgr2gray(img)\notsu = cvh.threshold_binary(gray, 250, inverse=True)\ncontours = cvh.find_external_contours(otsu)\ncontour = contours[0]\nrect = contour.bounding_rect # Gives a Rect object\ndegrees = 60\n\ncenter = rect.center # Gives a Point object\ntop_left = rect.tl # Gives a Point object\nnew_center = center - top_left \nimg[rect.slice] = cvh.rotate_image(\n img[rect.slice], new_center, degrees, unit=cvh.AngleUnit.DEGREES\n)\ncvh.rectangle(img, rect, cvh.Color.RANDOM)\n\ncv.imshow(\"Image\", img)\ncvh.wait_key(0)\n```\nWe again follow the same approach. However, with the Contour class, we can\nsimply call the bounding rect property. This yields a Rect object, which\nhas a center property. Convenient. \n\nWhere we before were left with no (obvious) choice but to rotate the whole image,\nwe can now simply slice the image at the rectangle, only rotating the figure itself.\nFor this exact purpose, it doesn't make much different, but it is a demonstration.\nWe find the new center from which to rotatet, and simply call the rotate image function. \nWe can here choose whether to use degrees or radians. Lastly we draw a rectangle with\na random color.\n\nWe get the following result.\n\n![alt text](https://raw.githubusercontent.com/anbergem/cvhelper/master/images/opencv.png)\n\nNot only is this a tad less tedious to write, but we are also easily able to \nrotate only the relevant part of the circle by slicing. The contour, rectangle\nand point objects are also an ease to work with. \n\n### Other Area of Ease\nWhile not providing examples, there are many other parts of the OpenCV \nthat become an ease to work with, when using cvhelper. Areas include\n\n* Morphology \n* Image normalization\n* Color conversion\n* Thresholding\n* Image smoothing\n\n\n\n\n", "description_content_type": "text/markdown", "docs_url": null, "download_url": "", "downloads": { "last_day": -1, "last_month": -1, "last_week": -1 }, "home_page": "https://github.com/anbergem/cvhelper", "keywords": "opencv", "license": "MIT", "maintainer": "", "maintainer_email": "", "name": "cvhelper", "package_url": "https://pypi.org/project/cvhelper/", "platform": "", "project_url": "https://pypi.org/project/cvhelper/", "project_urls": { "Homepage": "https://github.com/anbergem/cvhelper" }, "release_url": "https://pypi.org/project/cvhelper/0.1.0/", "requires_dist": [ "numpy (<=1.15.3)", "opencv-python (<=3.4.3.18)" ], "requires_python": ">=3.7", "summary": "A helper package for OpenCV", "version": "0.1.0" }, "last_serial": 4966892, "releases": { "0.1.0": [ { "comment_text": "", "digests": { "md5": "73eee772a691cf5b28899b0a8f530f1b", "sha256": "6e1cfe2c5b2601befedd117d12a6bc61f9ae5218f1a9a7a47f987f5d581184c0" }, "downloads": -1, "filename": "cvhelper-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "73eee772a691cf5b28899b0a8f530f1b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=3.7", "size": 12636, "upload_time": "2019-03-21T07:25:20", "url": "https://files.pythonhosted.org/packages/4e/81/82dc92948e37adf3e8fe0b082447351522e15d141798a2e52cf0414f9b4c/cvhelper-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "36a8b0100f3b4db65a39b20cf4c14da1", "sha256": "226fc99a5c315bd8987d35f85cf5ae3f811b74be758a723e70eca0201b1c6ddb" }, "downloads": -1, "filename": "cvhelper-0.1.0.tar.gz", "has_sig": false, "md5_digest": "36a8b0100f3b4db65a39b20cf4c14da1", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.7", "size": 12904, "upload_time": "2019-03-21T07:25:22", "url": "https://files.pythonhosted.org/packages/c0/f2/e11a47e041d6b969b124da8b89115539afa7e3d3b3b3d25c0489c5160446/cvhelper-0.1.0.tar.gz" } ] }, "urls": [ { "comment_text": "", "digests": { "md5": "73eee772a691cf5b28899b0a8f530f1b", "sha256": "6e1cfe2c5b2601befedd117d12a6bc61f9ae5218f1a9a7a47f987f5d581184c0" }, "downloads": -1, "filename": "cvhelper-0.1.0-py2.py3-none-any.whl", "has_sig": false, "md5_digest": "73eee772a691cf5b28899b0a8f530f1b", "packagetype": "bdist_wheel", "python_version": "py2.py3", "requires_python": ">=3.7", "size": 12636, "upload_time": "2019-03-21T07:25:20", "url": "https://files.pythonhosted.org/packages/4e/81/82dc92948e37adf3e8fe0b082447351522e15d141798a2e52cf0414f9b4c/cvhelper-0.1.0-py2.py3-none-any.whl" }, { "comment_text": "", "digests": { "md5": "36a8b0100f3b4db65a39b20cf4c14da1", "sha256": "226fc99a5c315bd8987d35f85cf5ae3f811b74be758a723e70eca0201b1c6ddb" }, "downloads": -1, "filename": "cvhelper-0.1.0.tar.gz", "has_sig": false, "md5_digest": "36a8b0100f3b4db65a39b20cf4c14da1", "packagetype": "sdist", "python_version": "source", "requires_python": ">=3.7", "size": 12904, "upload_time": "2019-03-21T07:25:22", "url": "https://files.pythonhosted.org/packages/c0/f2/e11a47e041d6b969b124da8b89115539afa7e3d3b3b3d25c0489c5160446/cvhelper-0.1.0.tar.gz" } ] }