"AI Basics" python:opencv--image arithmetic Operations (2): Bitwise operations

Source: Internet
Author: User
Tags arithmetic bitwise

Bitwise Operations

Bitwise operations are: And,or,not,xor and so on. When we extract part of an image, these operations are useful when choosing a non-rectangular ROI, often used for logo projection.

How to operate this section:
The image fixed threshold value of two is valued by the threshold function.
Definition: The two value of the image is to set the gray value of the pixel on the image to 0 or 255, that is, the entire image will be visible only black and white visual effects.
An image includes the target object, background and noise, in order to extract the target object directly from the multi-valued digital image, the common method is to set a threshold T, using t to divide the image data into two parts: a group of pixels larger than t and a pixel group less than T. This is the most special method to study the gray-scale transformation, called the image of the two value (binarization).

import cv2import numpy as npimg = cv2.imread(‘1.jpg‘)img2  = cv2.imread(‘2c.jpg‘)rows,cols,channels = img2.shaperoi = img[0:rows, 0:cols]GrayImage=cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY)  # 中值滤波  GrayImage= cv2.medianBlur(GrayImage,5)  # mask_bin 是黑白掩膜ret,mask_bin  = cv2.threshold(GrayImage,127,255,cv2.THRESH_BINARY)  #mask_inv 是反色黑白掩膜mask_inv = cv2.bitwise_not(mask)# 黑白掩膜 和 大图切割区域 取和img1_bg = cv2.bitwise_and(roi,roi,mask = mask_bin)#反色黑白掩膜 和 logo 取和img2_fg = cv2.bitwise_and(img2,img2,mask = mask_inv)dst = cv2.add(img1_bg,img2_fg) img[0:rows, 0:cols ] = dstcv2.imshow(‘GrayImage‘,mask_bin)cv2.waitKey(0)cv2.destroyAllWindows()

For ease of understanding, paste the process diagram

Mask_bin:

MASK_INV:

IMG1_BG and IMG2_FG:

Eventually:

"AI Basics" python:opencv--image arithmetic Operations (2): Bitwise operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.