GithubHelp home page GithubHelp logo

lau1944 / colorblue_control_object Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 923 KB

Using OpenCV2 detects blue color , control images position on screen (put the right color in the box !)

Python 100.00%
opencv python

colorblue_control_object's Introduction

ColorBlue_Control_Object

Using OpenCV2 detects blue color , control images position on screen (put the right color in the box !)

利用颜色控制窗口内目标图片

A Game Using OpenCV2 To Control Images

After detect color blue, a red point will display on the center of the color blue

Move the red point to the images slowly and control the images !

使用时将蓝色中心区域的红点对准图片,缓慢移动图片

Demo:

Code Explain

  • Step 1 .Display Images and Rectangle box

Randomly produce imageY in three places (50, 250 , 450)

#generate random position for those three colors
blue_dis = random.randrange (50,450,200)
if blue_dis == 450:
    yellow_dis = random.randrange(50,250,200)
    if yellow_dis == 50:
        green_dis = 250
    if yellow_dis == 250:
        green_dis = 50   
elif blue_dis == 50:
    yellow_dis = random.randrange(250,450,200)
    if yellow_dis == 250:
        green_dis = 450
    if yellow_dis == 450:
        green_dis = 250   
elif blue_dis == 250:
    yellow_dis = random.randrange(50,450,400)
    if yellow_dis == 50:
        green_dis = 450
    if yellow_dis == 450:
        green_dis = 50   

display images and rectangle box

    #insert logo function with random position
    InsertLogo_2(frame,blue_img,yBluelogo,blue_dis)
    InsertLogo_2(frame,green_img,yGreenlogo,green_dis)
    InsertLogo_2(frame,yellow_img,yYellowlogo,yellow_dis)
    
    # yellow box
    cv2.rectangle(frame, (50,350), (150, 450),(255,255,0), 3)
    # blue box
    cv2.rectangle(frame, (200,350), (300, 450),(30,144,255), 3)
    # green box
    cv2.rectangle(frame, (400,350), (500, 450),(124,252,0), 3)
  • Step 2 .Detect Blue Color
  #find blue in webcam
    centerBlue = None
    cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    if len(cnts) > 0:
	    c = max(cnts, key=cv2.contourArea)
	    ((xBlue, yBlue), radius) = cv2.minEnclosingCircle(c)
	    M = cv2.moments(c)
      # center blue location
	    xBlue=int(M["m10"] / M["m00"])
	    yBlue=int(M["m01"] / M["m00"])
	    centerBlue = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
	    if radius > 10:
		    mode = False
		    cv2.circle(frame, centerBlue, 5, (0, 0, 255), -1)  
  • Step 3 .Control the images
#controller !!
    if xBlue >= blue_dis and xBlue <= blue_dis + 100 :
        if yBlue >= yBluelogo and yBlue <= yBluelogo+100 :
            yBluelogo = yBlue
            # xBlue is the central of blue part
            # -50 in order to let imageX display on the red point
            blue_dis = xBlue-50
    if xBlue >= yellow_dis and xBlue <= yellow_dis + 100 :
        if yBlue >= yYellowlogo and yBlue <= yYellowlogo+100 :
            yYellowlogo = yBlue
            yellow_dis = xBlue-50
    if xBlue >= green_dis and xBlue <= green_dis + 100 :
        if yBlue >= yGreenlogo and yBlue <= yGreenlogo+100 :
            yGreenlogo = yBlue
            green_dis = xBlue-50       

Thank You !

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.