在Python中使用OpenCV裁剪图像

What is Cropping?

什么是播种?

Cropping is the removal of unwanted outer areas from a photographic or illustrated image. The process usually consists of the removal of some of the peripheral areas of an image to remove extraneous trash from the picture, to improve its framing, to change the aspect ratio, or to accentuate or isolate the subject matter from its background.

裁剪是从摄影或插图图像中去除不需要的外部区域。 该过程通常包括去除图像的某些外围区域,以从图片中去除多余的垃圾,以改善其取景,改变纵横比,或者使主题与背景突出或隔离。

We will be using these functions of OpenCV - python(cv2),

我们将使用OpenCV的这些功能-python(cv2),

  1. imread(): This function is like it takes an absolute path of the file and reads the whole image, and after reading the whole image it returns us the image and we will store that image in a variable.

    imread() :此函数就像它采用文件的绝对路径并读取整个图像一样,在读取整个图像之后,它将返回图像并将图像存储在变量中。

  2. imshow(): This function will be displaying a window (with a specified window name) which contains the image that is read by the imread() function.

    imshow() :此函数将显示一个窗口(具有指定的窗口名称),该窗口包含由imread()函数读取的图像。

  3. shape: This function will return the height, width, and layer of the image

    形状 :此函数将返回图像的高度宽度

Let’s take an example,

让我们举个例子

Let there be a list a=[1,2,3,4,5,6,7,8,9]Now, here I just wanted the elements between 4 and 8
(including 4 and 8) so what we will do is :print(a[3:8])
The result will be like : [4,5,6,7,8]

裁剪图像的Python程序 (Python program to crop an image)

# importing the module
import cv2
img=cv2.imread("/home/abhinav/PycharmProjects/untitled1/a.jpg")
# Reading the image with the help of
# (specified the absolute path)
# imread() function and storing it in the variable img
cv2.imshow("Original Image",img)
# Displaying the Original Image Window 
# named original image
# with the help of imshow() function
height,width=img.shape[:2]
# storing height and width with the help
# of shape function as shape return us
# three things(height,width,layer) in the form of list
# but we wanted only height and width
start_row,start_col=int(width*0.25),int(height*0.25)
end_row,end_col=int(width*0.75),int(height*0.75)
# start_row and start_col are the cordinates 
# from where we will start cropping
# end_row and end_col is the end coordinates 
# where we stop
cropped=img[start_row:end_row,start_col:end_col]
# using the idexing method cropping 
# the image in this way
cv2.imshow("Cropped_Image",cropped)
# using the imshow() function displaying 
# another window of
# the cropped picture as cropped contains 
# the part of image
cv2.waitKey(0)
cv2.destroyAllWindows()

Output:

输出:

Python | cropping an image

You can see the Cropped Image and the Original Image(the cropping is done like 0.25 to 0.75 with row and with column 0.25 to 0.75, and you can change the number).

您可以看到“裁剪的图像”和“原始图像”(裁剪的过程类似于0.25到0.75行和0.25到0.75列,并且可以更改数字)。

翻译自: https://www.includehelp.com/python/cropping-an-image-using-opencv.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544116.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

面渣逆袭:RocketMQ二十三问

1.为什么要使用消息队列呢?消息队列主要有三大用途,我们拿一个电商系统的下单举例:解耦:引入消息队列之前,下单完成之后,需要订单服务去调用库存服务减库存,调用营销服务加营销数据……引入消息…

vue项目打包体积大优化之-productionSourceMap设置

一、productionSourceMap 的作用 productionSourceMap 在构建时生成完整的 SourceMap 文件,默认情况下开启。生产环境中启用 productionSourceMap 有助于开发者调试代码,可以在浏览器的调试工具中查看到源文件中错误的代码位置,而不是编译后…

Java日志性能那些事(转)

在任何系统中,日志都是非常重要的组成部分,它是反映系统运行情况的重要依据,也是排查问题时的必要线索。绝大多数人都认可日志的重要性,但是又有多少人仔细想过该怎么打日志,日志对性能的影响究竟有多大呢?…

如何在Java中使ArrayList只读?

使ArrayList只读 (Making ArrayList Read-Only) Given an ArrayList, and we have to make it Read-Only in Java. 给定一个ArrayList,我们必须使其成为Java只读。 Read-Only: If we make ArrayList as Read-Only i.e. we can only read ArrayList and we cannot p…

33岁程序员的年中总结

作者 | 磊哥来源 | Java中文社群(ID:javacn666)转载请联系授权(微信ID:GG_Stone)人生在不同的阶段会有不同的生活方式和思考问题的角度,这是一件非常有趣的事~ 比如,我在 22 岁会想&…

减治求有重复元素的全排列

求n个元素的全排列的所有解可以用减治法:每次拎出一个数做前缀,对剩下的元素再求全排列,直至只剩一个元素。代码源自《算法分析与设计(王晓东)》,复杂度O(n!) 1 //输出k~m的所有全排列2 void pe…

数据科学中的简单线性回归

简单线性回归 (Simple Linear Regression) A simple regression model could be a linear approximation of a causative relationship between two or additional variables. Regressions models are extremely valuable, as theyre one in every of the foremost common ways…

鹅厂一面,有关 ThreadLocal 的一切

1. 底层结构ThreadLocal 底层有一个默认容量为 16 的数组组成,k 是 ThreadLocal 对象的引用,v 是要放到 TheadLocal 的值public void set(T value) {Thread t Thread.currentThread();ThreadLocalMap map getMap(t);if (map ! null)map.set(this, valu…

再战“超融合”,戴尔、Nutanix绝世好CP

从进入PC领域开始,戴尔一直在扮演颠覆者的角色。戴尔的理想是以开放、标准化的技术和解决方案颠覆传统的封闭的技术和市场,实现与合作伙伴的共赢。在超融合架构逐渐兴起的今天,戴尔依旧希望以变革者的身份,携手超融合架构的先驱Nu…

ruby array_Ruby中带有示例的Array.index()方法

ruby arrayArray.index()方法 (Array.index() Method) In this article, we will study about Array.index() method. You all must be thinking the method must be doing something which is related index of certain element. It is not as simple as it looks. Well, we w…

面试突击58:truncate、delete和drop的6大区别!

作者 | 磊哥来源 | Java面试真题解析(ID:aimianshi666)转载请联系授权(微信ID:GG_Stone)在 MySQL 中,使用 truncate、delete 和 drop 都可以实现表删除,但它们 3 个的使用场景和执行…

scala 去除重复元素_Scala程序从列表中删除重复项

scala 去除重复元素List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable data structure but may contain duplicate elements. And in real life implementation duplicate elements increase the runtime of the program…

智力游戏

【Description】whitecloth 最近迷上了一个你小时候已经玩厌了的游戏:移火柴棒。他现在吵着要你陪他玩,你没有办法,只好写一个程序来完成这个工作了。你被给出了一个火柴拼成的等式,比如说下面这个:( 5 7 …

面渣逆袭:MySQL六十六问!建议收藏

基础MySQ Logo作为SQL Boy,基础部分不会有人不会吧?面试也不怎么问,基础掌握不错的小伙伴可以跳过这一部分。当然,可能会现场写一些SQL语句,SQ语句可以通过牛客、LeetCode、LintCode之类的网站来练习。1. 什么是内连接…

C ++中带有示例的llabs()函数

C llabs()函数 (C llabs() function) llabs() function is a library function of cstdlib header. It used to get the absolute of the given value. This function is similar to the abs() and labs() functions except for the type of the parameter, it is used for th…

Mysql+Heartbeat+Drbd生产环境高可用部署若干问题解惑

MysqlHeartbeatDrbd生产环境高可用部署若干问题解惑:############################################################## Purpose: MysqlHeartbeatdrbd高可用部署中学生的几个疑惑解答## USER YYYY-MM-DD – ACTION # Oldboy 2011-3-14 – Created# …

try-with-resources 中的一个坑,注意避让

小伙伴们好呀,昨天复盘以前做的项目(大概有一年了),看到这个 try-catch ,又想起自己之前掉坑的这个经历 ,弄了个小 demo 给大家感受下~ 😄问题1一个简单的下载文件的例子。这里会出现什么情况…

c++ abort 函数_C ++中带有示例的abort()函数

c abort 函数C abort()函数 (C abort() function) abort() function is a library function of cstdlib header. It is used to abort the current process. For the abnormal program termination – we can use abort() function. abort()函数是cstdlib标头的库函数。 用于中…

第 二 十 八 天 :LB 负 载 均 衡 搭 建 之 LVS

小Q:抱怨,是一种负能量,犹如搬起石头砸自己的脚,与人无益,于己不利,于事无补 前面我们介绍了HA高可用集群,今天我们来了解下LB负载均衡集群,在学习完基本的搭建后,在扩展…

一个依赖搞定Spring Boot 配置文件脱敏

经常会遇到这样一种情况:项目的配置文件中总有一些敏感信息,比如数据源的url、用户名、密码....这些信息一旦被暴露那么整个数据库都将会被泄漏,那么如何将这些配置隐藏呢?今天介绍一种方案,让你在无感知的情况下实现配…